saxon:namespace-node($prefix as xs:string, $uri as xs:string) ==> node()

This function creates a new namespace node. The first argument gives the name of the namespace node (that is, the namespace prefix), while the second gives the namespace URI. The prefix may be "" to create a default namespace; otherwise it must be a valid NCName. The URI must not be the empty string.

The function serves the same role in XQuery as the xsl:namespace instruction in XSLT 2.0: it allows a namespace in the result document to be computed dynamically.

The namespace node is typically used as part of the computed content of a constructed element node. For example:

declare namespace saxon="";
<a xsi:type="my:decimal">
  { saxon:namespace-node("my", "http://my.uri/"), 12.4 }

produces the output:

<a xmlns:my="http://my.uri/" 

In this case the namespace node could equally well be created by adding the namespace declaration xmlns:my="http://my.uri/" to the direct element constructor for the <a> element. But this is not always possible, for example (a) if the element name is not known statically, or (b) if the namespace URI is not known statically, or (c) if the decision whether or not to add the namespace node depends on input data.

Note: the use case for this function relies on an extension to the semantics of XQuery element construction. Saxon treats a namespace node appearing in the content of an element constructor according to the XSLT rules: that is, the namespace node is added to the containing element in the same way as attributes are added. Attributes and namespaces may appear in any order, but must precede any child nodes.