import ballerina/io;

public function main() {
    // An XML element. There can be only one root element.
    xml x1 = xml `<book>The Lost World</book>`;
    io:println(x1);

    // An XML text.
    xml x2 = xml `Hello, world!`;
    io:println(x2);

    // An XML comment.
    xml x3 = xml `<!--I am a comment-->`;
    io:println(x3);

    // An XML processing instructions.
    xml x4 = xml `<?target data?>`;
    io:println(x4);

    // Multiple XML items can be combined to form a sequence of XML.
    // The resulting sequence is another XML on its own.
    xml x5 = x1 + x2 + x3 + x4;
    io:println(x5);
}

XML Data Model

An xml value is a sequence representing the parsed content of an XML element.

An xml value has four kinds of items.

  • element, processing instruction and comment item correspond 1:1 to XML infoset items
  • text item corresponds to one or more Character Information Items


XML document is an xml sequence with only one element and no text. An element item is mutable and consists of:

  • name: type string
  • attributes: type map<string>
  • children: type xml


A text item is immutable.

  • it has no identity: == is the same as ===
  • consecutive text items never occur in an xml value: they are always merged

import ballerina/io;
public function main() {
    xml x1 = xml `<book>The Lost World</book>`;
    io:println(x1);

An XML element. There can be only one root element.

    xml x2 = xml `Hello, world!`;
    io:println(x2);

An XML text.

    xml x3 = xml `<!--I am a comment-->`;
    io:println(x3);

An XML comment.

    xml x4 = xml `<?target data?>`;
    io:println(x4);

An XML processing instructions.

    xml x5 = x1 + x2 + x3 + x4;
    io:println(x5);
}

Multiple XML items can be combined to form a sequence of XML. The resulting sequence is another XML on its own.

bal run xml.bal
<book>The Lost World</book>
Hello, world!
<!--I am a comment-->
<?target data?>
<book>The Lost World</book>Hello, world!<!--I am a comment--><?target data?>