How to parse an xml file into a variable

By preloading xml data into variables you can use the data in memory in multiple operations without parsing it over and over.
XML variables are stored internally in a tree structure which is shared by most of the core commands so needs no additional processing.
XML variable size is only limited by the JVM memory, and works exactly the same as using standard xml processing tools such as xlst and xquery. It is no less efficient to use an XML variable for an entire document then to process it with any of the commands which also have to load the document into memory.


The xread command reads a file (or other input) into a variable.
For example, to read the file "myfile.xml" into the variable "myfile"

xread myfile < myfile.xml

Since xread reads from standard input, it can also read from the the output of a pipeline.

For example, to parse the output of the xls command into the "mydir" variable.
xls | xread mydir

XML Process substitution

Another way to store XML data in a variable is using xml process substitution.
This is a variant on the unix shell text process substitution $(command). An XML process substitution uses the syntax $<(command).
The difference is that XML process substitution parses the output of the command an XML document and returns it as an XML expression value.

For example, to assign the variable "mydir" from the output of the xls command using xml process substitution.


There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki