XML Module

The nzLua XML module is a implemented as a document object model (DOM) XML parser. An XML object keeps track of the current position within the document. The position can be moved to other nodes in the document by using the various methods available in the XML object. Other methods can be used to get information about the current node.

str = [[
<config version="1.0">
        <option type="type1">value1</option>
        <option type="type2">value2</option>
</config>
]]

doc = xml.parse(str)
value1 = doc:getText('/config/option[2]')      ==> 'value2'
doc:goPath('/config/option')
value2 = doc:text()                            ==> 'value1'

Many of the XML methods accept a document path as an argument. The path in nzLua is a limited subset of the standard XPath syntax. The nzLua path syntax does not support pattern matching, expressions, or filter conditions. The options available for an XML path within nzLua are listed in the following table.

Table 1. XML Path Options
Expression Description
/ The root node.
. The current node.
.. Parent of the current node.
/foo The node named foo, which is a child of the Root node.
/foo/bar The node bar, which is a child of the node foo, which is a child of the root node.
foo The first node named foo, which is a child of the current node.
foo[3] The third node named foo, which is a child of the current node.
/foo/bar[2] The second element named bar, which is a child of the root node foo.
/foo/bar[last()] The last element named bar, which is a child of the root node foo.
./bar The node bar, which is a child of the current node.