Swan Lake Beta3 is here.
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.<para>` - every element in `x` named `para`.
    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/<para>` - for every element `e` in `x`, every element named para in the children of `e`.
    xml c = x/<planner>;
    io:println(c);

    // `x/<th|td>` - for every element `e` in `x`, every element named `th` or `td` 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/**/<para>  - for every element `e` in `x`, every element named `para` in 
    // the descendants of `e`.
    xml f = x/**/<name>;
    io:println(f);

    // x/<para>[0]  - for every element `e` in `x`, first element named `para` 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.<para> - every element in x named para.

    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/<para> - for every element e in x, every element named para in the children of e.

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

x/<th|td> - for every element e in x, every element named th or td 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 para in the descendants of e.

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

x/[0] - for every element e in x, first element named para 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>
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.<para>` - every element in `x` named `para`.
    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/<para>` - for every element `e` in `x`, every element named para in the children of `e`.
    xml c = x/<planner>;
    io:println(c);

    // `x/<th|td>` - for every element `e` in `x`, every element named `th` or `td` 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/**/<para>  - for every element `e` in `x`, every element named `para` in 
    // the descendants of `e`.
    xml f = x/**/<name>;
    io:println(f);

    // x/<para>[0]  - for every element `e` in `x`, first element named `para` 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.<para> - every element in x named para.

    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/<para> - for every element e in x, every element named para in the children of e.

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

x/<th|td> - for every element e in x, every element named th or td 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 para in the descendants of e.

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

x/[0] - for every element e in x, first element named para 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>
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand