import ballerina/io;

public function main() {
    xml x = xml 
        `<items>
            <!--Contents-->
            <book>
                <name>A Study in Scarlet</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <planner>Daily Planner<kind>day</kind><pages>365</pages></planner>
            <book>
                <name>The Sign of Four</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <pen><kind>marker</kind><color>blue</color></pen>
        </items>`;

    // `x.<items>` - every element in `x` named `items`.
    xml a = x.<items>;
    io:println(a);

    // `x/*` - for every element `e` in `x`, the children of `e`.
    xml b = x/*;
    io:println(b);

    // `x/<planner>` - for every element `e` in `x`, every element named `planner` in the children of `e`.
    xml c = x/<planner>;
    io:println(c);

    // `x/<planner|pen>` - for every element `e` in `x`, every element named `planner` or `pen` in the
    // children of `e`.
    xml d = x/<planner|pen>;
    io:println(d);

    // `x/<*>` - for every element `e` in `x`, every element in the children of `e`.
    xml e = x/<*>;
    io:println(e);

    // x/**/<name>  - for every element `e` in `x`, every element named `name` in
    // the descendants of `e`.
    xml f = x/**/<name>;
    io:println(f);

    // x/<book>[0]  - for every element `e` in `x`, first element named `book` in
    // the children of `e`.
    xml g = x/<book>[0];
    io:println(g);

}

XML navigation

XML navigation expressions allow for convenient navigation of XML element structure, in a similar way to XPath.

import ballerina/io;
public function main() {
    xml x = xml 
        `<items>
            <!--Contents-->
            <book>
                <name>A Study in Scarlet</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <planner>Daily Planner<kind>day</kind><pages>365</pages></planner>
            <book>
                <name>The Sign of Four</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <pen><kind>marker</kind><color>blue</color></pen>
        </items>`;
    xml a = x.<items>;
    io:println(a);

x.<items> - every element in x named items.

    xml b = x/*;
    io:println(b);

x/* - for every element e in x, the children of e.

    xml c = x/<planner>;
    io:println(c);

x/<planner> - for every element e in x, every element named planner in the children of e.

    xml d = x/<planner|pen>;
    io:println(d);

x/<planner|pen> - for every element e in x, every element named planner or pen in the children of e.

    xml e = x/<*>;
    io:println(e);

x/<*> - for every element e in x, every element in the children of e.

    xml f = x/**/<name>;
    io:println(f);

x/**/ - for every element e in x, every element named name in the descendants of e.

    xml g = x/<book>[0];
    io:println(g);

x/[0] - for every element e in x, first element named book in the children of e.

}
bal run xml_navigation.bal
<items>
            <!--Contents-->
            <book>
                <name>A Study in Scarlet</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <planner>Daily Planner<kind>day</kind><pages>365</pages></planner>
            <book>
                <name>The Sign of Four</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <pen><kind>marker</kind><color>blue</color></pen>
        </items>
            <!--Contents-->
            <book>
                <name>A Study in Scarlet</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <planner>Daily Planner<kind>day</kind><pages>365</pages></planner>
            <book>
                <name>The Sign of Four</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>
            <pen><kind>marker</kind><color>blue</color></pen>
        
<planner>Daily Planner<kind>day</kind><pages>365</pages></planner>
<planner>Daily Planner<kind>day</kind><pages>365</pages></planner><pen><kind>marker</kind><color>blue</color></pen>
<book>
                <name>A Study in Scarlet</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book><planner>Daily Planner<kind>day</kind><pages>365</pages></planner><book>
                <name>The Sign of Four</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book><pen><kind>marker</kind><color>blue</color></pen>
<name>A Study in Scarlet</name><name>Arthur Conan Doyle</name><name>The Sign of Four</name><name>Arthur Conan Doyle</name>
<book>
                <name>A Study in Scarlet</name>
                <author><name>Arthur Conan Doyle</name></author>
            </book>