Wiki source for XmlExpressions

Show raw source

======XML Expressions======
XML Expressions are expressions that construct or return XML [[ XDM]] values including sequences.
These expressions retain an internal parsed representation wherever possible, including when assigned to variables or passed
as arguments to builtin or internal commands.

=====XML Construction=====
XML Construction is introduced by using the <[ ]> syntax.
For example, to construct a single xsd:integer value

To construct a sequence of integer,string, and XML node
<[1,"hi there",<foo>bar</foo>]>

====XQuery expressions====
XML Construction is implemented by using XQuery, so any XQuery expression is valid in an XML constructor expression.
For technical specifications of XQuery see [[ XQuery 1.0: An XML Query Language]]
XQuery 1.0 supports all the XQuery 1.0 and XPath 2.0 functions. See [[ XQuery 1.0 and XPath 2.0 Functions and Operators]]

For example FLWOR expressions are supported
for $d in 1 to 10
let $n := <int>{$d}</int>
return $n
To see this in action, you can assign this to a variable and echo it.
$ a=<[
for $d in 1 to 10
let $n := <int>{$d}</int>
return $n
$ echo $a
<int>1</int> <int>2</int> <int>3</int> <int>4</int> <int>5</int> <int>6</int>
<int>7</int> <int>8</int> <int>9</int> <int>10</int>

XML Expressions, like String expressions have access to environment variables.
$ A=string
$ B=<[<node/>]>
$ echo <[<element attr="{$A}">{$B}</element>]>
<element attr="string">

===Parameters and Variables===
Positional parameters ($1 ... $N) and all environment variables with the XEXPR flag set are imported automatically.
Positional parameters are imported as variables named $_1 , $_2 ...
Environment variables are imported as their own name e,g $PATH, $TMPDIR

By default only variables assigned within xmlsh inherit the XEXPR flag. The intent is to avoid polluting the global namespace of the xquery expression, and to avoid unnecessary overhead.

The xml namespace is also predeclared and mapped to the XML extension functions.
Additionally all in scope namespaces are predeclared.

This is implemented by prepending a set of XQuery statements to every invocation of <[ ]>
For example suppose the environment contains the variables A and B with the XEXPR flag, and 2 positional parameters.
The generated xquery script for <[ $A/foo ]> looks like

declare namespace xmlsh="";
[ additional in-scope namespace declarations ]
declare variable $A external ;
declare variable $B external ;
declare variable $_1 external ;
declare variable $_2 external ;

The benefit of this is that shell variables, parameters, and namespaces are pre-declared with no additional syntax necessary.
However a side effect is that you cannot declare additional namespaces explicitly, or import modules, and there is some overhead
declaring and assigning variables which you may not use.

To have finer control over xquery execution use the [[CommandXQuery xquery]] command.
For example to only pass in the A variable and use the same expression as above

xquery -q 'declare variable $A external; $A/foo' -n -v A $A

=====XML Process substition=====
Any command that produces an XML document (but not sequences > 1 value) can be evaulated using the $<( command )> syntax.
$ files=$<(xls)>

====Reading XML Documents====
Entire XML documents stored in files can be read into variables using the [[CommandXread xread]] command.

Read an xml docment into variable a
xread a < file.xml

Valid XHTML :: Valid CSS: :: Powered by WikkaWiki