Swan Lake Beta2 is here.
import ballerina/io;

type Person record {|
    string name;
    string country;
|};

function personsToXml(Person[] persons) returns xml {
    // Uses a template containing a query expression, which also contains a template.
    return xml`<data>${from var {name, country} in persons
           select xml`<person country="${country}">${name}</person>`}</data>`;

}

public function main() {
    Person[] persons = [
        {name: "Jane", country: "USA"},
        {name: "Mike", country: "Germany"}
    ];
    io:println(personsToXml(persons));
}

XML Templates and Query

XML templates can be combined with queries (e.g., you can have a template containing a query expression, which also contains a template).

import ballerina/io;
type Person record {|
    string name;
    string country;
|};
function personsToXml(Person[] persons) returns xml {
    return xml`<data>${from var {name, country} in persons
           select xml`<person country="${country}">${name}</person>`}</data>`;

Uses a template containing a query expression, which also contains a template.

}
public function main() {
    Person[] persons = [
        {name: "Jane", country: "USA"},
        {name: "Mike", country: "Germany"}
    ];
    io:println(personsToXml(persons));
}
bal run xml_templates_and_query.bal
<data><person country="USA">Jane</person><person country="Germany">Mike</person></data>
import ballerina/io;

type Person record {|
    string name;
    string country;
|};

function personsToXml(Person[] persons) returns xml {
    // Uses a template containing a query expression, which also contains a template.
    return xml`<data>${from var {name, country} in persons
           select xml`<person country="${country}">${name}</person>`}</data>`;

}

public function main() {
    Person[] persons = [
        {name: "Jane", country: "USA"},
        {name: "Mike", country: "Germany"}
    ];
    io:println(personsToXml(persons));
}

XML Templates and Query

XML templates can be combined with queries (e.g., you can have a template containing a query expression, which also contains a template).

import ballerina/io;
type Person record {|
    string name;
    string country;
|};
function personsToXml(Person[] persons) returns xml {
    return xml`<data>${from var {name, country} in persons
           select xml`<person country="${country}">${name}</person>`}</data>`;

Uses a template containing a query expression, which also contains a template.

}
public function main() {
    Person[] persons = [
        {name: "Jane", country: "USA"},
        {name: "Mike", country: "Germany"}
    ];
    io:println(personsToXml(persons));
}
bal run xml_templates_and_query.bal
<data><person country="USA">Jane</person><person country="Germany">Mike</person></data>

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