xmlnodes

The [xmlnodes] context is to iterate the child XML nodes of a parent node. The location of the parent node, in the xml 'tree', is determined by the 'path' parameter. If a path parameter is not provided, then the child nodes of the ref 's root are iterated.

Parameters

ParameterDescription
refReference to an xml parsed object variable. If this parameter is not provided, then it is assumed that there is an 'outer' [xmlnodes] context from which to reference a particular XML node. (This is explained further on in this section).
pathDepending on which path method is used (see below), this will either be a path to the parent xml element (node) from which to iterate the child elements (nodes), or a path 'expression' representing a collection of XML nodes. If a path is not provided, then the child nodes of the 'ref' node will be iterated
nameA string used to filter the resulting xml nodes. Only the xml nodes that match the 'name' string will be iterated.
exactUsed with the 'name' parameter. Either 'T' or 'F'. Specifies whether the 'name' parameter is a 'whole' string match or a 'sub-string' match.

Tags available within a [xmlnodes] context:

TagDescription
indexThe 'count' of the current iterated node.
nameThe name of the current iterated XML node.
valueThe value of the current iterated XML node. Will be empty if the node is a 'container' node, i.e. contains other XML nodes.
numfoundThe total 'count' of iterated nodes.
contentThe 'raw' xml content of the current node.
iscontainer'T' if the current node contains other XML nodes.
The first step to obtaining the XML nodes is to first parse the XML content using the WebDNA [xmlparse] context.

Obtaining the path parameter from the parsed XML can use three different methods: named, indexed, or xpath.

The named method expresses a literal path to the parent node, i.e. path=named:CATALOG/CD(n). If there are more than one similarly named 'sibling' nodes, then the '(n)' part specifies which node to select as part of the path.

The indexed method expresses a numerical "stepped" value path to the parent node, i.e. path=indexed:1/2/3. This example could be expressed as: 'The third child node of the second child node of the first child node of the xml root'.

The xpath method is an XPath 'expression' that evaluates to a collection of nodes in the XML tree. In this case, the iterated nodes are those of the resulting 'collection' of nodes. This is a bit different from the 'named' and 'indexed' method in that the collection of node are not the 'child' nodes of a given 'parent' node. This is the most powerful method for selecting XML nodes. There are several online 'xpath' tutorials that you can visit that will help you develop your XPath skills.

Example WebDNA code:
This example uses an example file that you can view/download here example-file.xml
[xmlparse var=xml_var1][include file=example-file.xml][/xmlparse]
[xmlnodes ref=xml_var1]
   [name] = [value]<br>
[/xmlnodes]
Results: This example finds the first node or the root of the XML document.
CATALOG =
The node named 'CATALOG' is the only child node from the root of the XML file. Notice that the 'value' is empty. This is because the 'CATALOG' node has no value, and is actually a 'container' node for other XML nodes. A 'value' will only be displayed for a 'leaf' XML node.

Expanding on the method of a named path to delve deeper into the file.
Example WebDNA code:
This example uses an example file that you can view/download here example-file.xml
[xmlparse var=xml_var1][include file=example-file.xml][/xmlparse]
[xmlnodes ref=xml_var1&path=named:Catalog]
  [name] = [value]<br>
[/xmlnodes]
Results: All the names of the child nodes of the parent 'CATALOG' are displayed. Again, none of the resulting child nodes contain a value as they are all 'container' nodes.
CD=
CD=
CD=
CD=
CD=

[xmlnodes] contexts can be embedded within [xmlnodes] contexts. This method will iterate the child nodes of all the 'CD' nodes of the example file.

Example WebDNA code:
This example uses an example file that you can view/download here example-file.xml
[xmlparse var=xml_var1][include file=example-file.xml][/xmlparse]
[xmlnodes ref=xml_var1&path=named:Catalog]
  <b>[name] - [index]</b><br>
  [xmlnodes]
    [name] = [value]<br>
  [/xmlnodes]<br>
[/xmlnodes]
Results: (only the first 4 are displayed here, there are may more in the file) Note that the 'inner' xmlnodes context did not need a 'ref' parameter. This is because the inner xmlnodes context inherited the outer xmlnodes' current iterated node. Also notice that the inner xmlnodes context did not use a 'path' parameter. So it uses the 'root' path of the outer xmlnodes' current iterated node. As with most 'iterative' WebDNA contexts, the [index] tag resolves to the current iteration 'count'.
CD - 1
  TITLE = Empire Burlesque
  ARTIST = Bob Dylan
  COUNTRY = USA
  COMPANY = Columbia
  PRICE = 10.90
  YEAR = 1985
CD - 2
  TITLE = Hide your heart
  ARTIST = Bonnie Tylor
  COUNTRY = UK
  COMPANY = CBS Records
  PRICE = 9.90
  YEAR = 1988
CD - 3
  TITLE = Greatest Hits
  ARTIST = Dolly Parton
  COUNTRY = USA
  COMPANY = RCA
  PRICE = 9.90
  YEAR = 1982
CD - 4
  TITLE = Still got the blues
  ARTIST = Gary More
  COUNTRY = UK
  COMPANY = Virgin records
  PRICE = 10.20
  YEAR = 1990

With a minor change to the 'path' parameter, we can retrieve all the child nodes of the fifth 'CD' node of example-file.xml, still using the named method.

Example WebDNA code:
This example uses an example file that you can view/download here example-file.xml
[xmlparse var=xml_var1][include file=example-file.xml][/xmlparse]
[xmlnodes ref=xml_var1&path=named:Catalog/CD(5)]
  [name] = [value]
[/xmlnodes]
Results:
TITLE = Eros
ARTIST = Eros Ramazzotti
COUNTRY = EU
COMPANY = BMG
PRICE = 9.90
YEAR = 1997

Once again using the named method the results can be filtered to display only the 'TITLE' node of the fifth 'CD' node of example-file.xml using the name parameter.

Example WebDNA code:
This example uses an example file that you can view/download here example-file.xml

[xmlparse var=xml_var1][include file=example-file.xml][/xmlparse]
[xmlnodes ref=xml_var1&path=named:Catalog/CD(5)&name=TITLE]
  [name] = [value]
[/xmlnodes]
Results:
TITLE = Eros

Further filtering is possible using the name and exact parameters, we can filter the results to display only those nodes, of the fifth 'CD' node, where the node name matches a given sub-string, 'CO' of example-file.xml

Example WebDNA code:
This example uses an example file that you can view/download here example-file.xml
[xmlparse var=xml_var1][include file=example-file.xml][/xmlparse]
[xmlnodes ref=xml_var1&path=named:Catalog/CD(5)&name=CO&exact=F]
  [name] = [value]
[/xmlnodes]
Results: Both node names start with CO, so that satisfies the exact=F
COUNTRY = EU
COMPANY = BMG