Swan Lake Beta3 is here.
// The identifier followed by the `as` keyword is the prefix bound
// to this namespace name.
xmlns "http://example.com" as eg;

xml x = xml`<eg:doc>Hello</eg:doc>`;

xmlns "http://example.com" as ex;

// `b` will be `true`.
boolean b = (x === x.<ex:doc>);

// `exdoc` will be `{http://example.com}doc`.
string exdoc = ex:doc;

XMLNS Declarations

The xmlns declarations are like import declarations, but bind the prefix to a namespace URL rather than a module. The xmlns declarations in the Ballerina module provide namespace context for parsing xml templates. The Qualified names in Ballerina modules are expanded into strings using the xmlns declarations in the module. The xmlns declarations are also allowed at block level.

xmlns "http://example.com" as eg;

The identifier followed by the as keyword is the prefix bound to this namespace name.

xml x = xml`<eg:doc>Hello</eg:doc>`;
xmlns "http://example.com" as ex;
boolean b = (x === x.<ex:doc>);

b will be true.

string exdoc = ex:doc;

exdoc will be {http://example.com}doc.

bal run xmlns_declarations.bal
// The identifier followed by the `as` keyword is the prefix bound
// to this namespace name.
xmlns "http://example.com" as eg;

xml x = xml`<eg:doc>Hello</eg:doc>`;

xmlns "http://example.com" as ex;

// `b` will be `true`.
boolean b = (x === x.<ex:doc>);

// `exdoc` will be `{http://example.com}doc`.
string exdoc = ex:doc;

XMLNS Declarations

The xmlns declarations are like import declarations, but bind the prefix to a namespace URL rather than a module. The xmlns declarations in the Ballerina module provide namespace context for parsing xml templates. The Qualified names in Ballerina modules are expanded into strings using the xmlns declarations in the module. The xmlns declarations are also allowed at block level.

xmlns "http://example.com" as eg;

The identifier followed by the as keyword is the prefix bound to this namespace name.

xml x = xml`<eg:doc>Hello</eg:doc>`;
xmlns "http://example.com" as ex;
boolean b = (x === x.<ex:doc>);

b will be true.

string exdoc = ex:doc;

exdoc will be {http://example.com}doc.

bal run xmlns_declarations.bal
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