import ballerina/io;public function main() {
    xml bookXML = xml `<book>
                <name>Sherlock Holmes</name>
                <author>
                    <fname title="Sir">Arthur</fname>
                    <mname>Conan</mname>
                    <lname>Doyle</lname>
                </author>
                <bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>
                <!--Price: $10-->
                </book>`;
    
    io:println(bookXML/<author>/<fname>);
    io:println(bookXML/<ISBN>/<code>);
    io:println(bookXML/<author>/<fname>.title);
    io:println(bookXML/**/<fname>);
    io:println(bookXML/*);
    io:println(bookXML/<*>);
    xmlns "http://ballerina.com/a" as bar;
    io:println(bookXML/<bar:*>/*);    xml seq = bookXML/*;
    io:println(seq.<name>);
    io:println(seq.<bar:year>);
}# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run xml-access.bal
<fname title="Sir">Arthur</fname>Sir
<fname title="Sir">Arthur</fname>                <name>Sherlock Holmes</name>
                <author>
                    <fname title="Sir">Arthur</fname>
                    <mname>Conan</mname>
                    <lname>Doyle</lname>
                </author>
                <bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>
                <!--Price: $10--><name>Sherlock Holmes</name><author>
                    <fname title="Sir">Arthur</fname>
                    <mname>Conan</mname>
                    <lname>Doyle</lname>
                </author><bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>
2019
<name>Sherlock Holmes</name>
<bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>

XML Access

XML elements can contain child XML items. Ballerina allows you to access these child elements conveniently using XML step expressions and XML filter expressions.

import ballerina/io;
public function main() {
    xml bookXML = xml `<book>
                <name>Sherlock Holmes</name>
                <author>
                    <fname title="Sir">Arthur</fname>
                    <mname>Conan</mname>
                    <lname>Doyle</lname>
                </author>
                <bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>
                <!--Price: $10-->
                </book>`;
    

The XML element with nested children.

    io:println(bookXML/<author>/<fname>);

You can access child XML items using XML step expressions.

    io:println(bookXML/<ISBN>/<code>);

Accessing a non-existing child will return an empty xml sequence.

    io:println(bookXML/<author>/<fname>.title);

You can also retrieve attributes of the resulting child XML.

    io:println(bookXML/**/<fname>);

You can match descendant elements using the following stepping access syntax.

    io:println(bookXML/*);

Select all the children elements using the below syntax.

    io:println(bookXML/<*>);

Select all children elements using the bellow syntax.

    xmlns "http://ballerina.com/a" as bar;
    io:println(bookXML/<bar:*>/*);

Select all the children belonging to a specific namespace.

    xml seq = bookXML/*;
    io:println(seq.<name>);
    io:println(seq.<bar:year>);
}

XML sequences can be filtered using XML filter expressions.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run xml-access.bal
<fname title="Sir">Arthur</fname>
Sir
<fname title="Sir">Arthur</fname>
                <name>Sherlock Holmes</name>
                <author>
                    <fname title="Sir">Arthur</fname>
                    <mname>Conan</mname>
                    <lname>Doyle</lname>
                </author>
                <bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>
                <!--Price: $10-->
<name>Sherlock Holmes</name><author>
                    <fname title="Sir">Arthur</fname>
                    <mname>Conan</mname>
                    <lname>Doyle</lname>
                </author><bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>
2019
<name>Sherlock Holmes</name>
<bar:year xmlns:bar="http://ballerina.com/a">2019</bar:year>