XML Node Selection & Referencing

XML Node Selection & Referencing

XPath provides multiple ways to reference and navigate through an XML structure.

Learn how to select specific nodes using direct references, index numbers, conditions, and relationships with other nodes.

Connecting preceding or following node sets to the main node set

Selecting a range of nodes using index/position()

Refer to a simple node in input XML file (XML reference).

XML reference via node number.

XML reference via a condition on a value.

Referencing a node via index number

Connecting preceding or following node sets to the main node set
In this example we will see how it is possible to connect detail node sets, that are found directly before or after the main nodeset and present them in a connected way in the output.

As an example we consider an XML file with a structure like below:

  

For each of the Document nodes there may be a Header and a Footer section. Both of them are optional, but the Header is always found just before the Document and the Footer is always found just after the Document.


It is possible to link the Header/Footer lines with the main Document node in a template like below:

  

In the setup above we create one separate page for each Document node and inside the page we select the Header lines with this repeat:

/Root/Header[following-sibling::Document[1]/DocumentNo=$DocNo]/Line

Previously the variable, DocNo is set to the current DocumentNo value, and the repeat selects the Header Line nodes for which the following sibling (to the Header node) contains the same value for DocumentNo as the current DocNo. In the repeat expression Document[1] is specified. In that way it verifies that the first of the following Document contains the matching value.

The same principle is used for the footer: Here we compare with the first of the preceding Document nodes.


The output is 3 pages like below:



Selecting a range of nodes using index/position()
There is in Xpath a function called position(). This function returns the current position (index or node number). This can e.g. be used, if you want to limit the number of nodes, that a repeat is to handle.

You can e.g. consider this in the designer, if you have loaded a sample file, that contains too many detail lines which can slow down the designer.

 

You can limit a repeat to a range of indexes (positions) like below. If you imagine this setup, which iterates across all detail lines:


 

Then you can change the repeat to only process Detail lines from 2 to 4 in this manner:


  

Inside the sharp brackets [ ], you can insert a condition tested for each detail line node. It can also be combined with other conditions with and/or.

Refer to a simple node in input XML file (XML reference).

In XPath you can e.g. refer to data in this XML file:


 

If you type this in InterFormNG2 as the XPath:

/Root/Greeting


Then you will get the value of this node, which here equals "HELLO WORLD".

"HELLO WORLD"
Notes

Please note that the Xpath is case sensitive, and xpath functions are written in lower case!

Starting with a ‘/’ indicates that you are referring to the node from the root of the XML file.


XML reference via node number.

You can refer to a node number in the tree.

As this document is the second you can also find the same contact person via this expression:
/Root/Document[2]/Contact_person
XML reference via a condition on a value.
You can verify, if a node contains a special text.

Here we check if a node contains the text 'HELLO':
boolean(/Root/Greeting[contains(.,'HELLO')])
Notes
The trick is, that the first parameter of contain is a dot, which refers to the current node.


Referencing a node via index number
You can refer to a node via the index if you e.g. have multiple nodes with the same in the input XML file.

If we e.g. consider an input file with multiple detail lines like below:


  

Then you can refer to a specific DetailLine node in e.g. a text element via the index like so:

/Root/Document/DetailLine[4]

 where this refers to the 4.th DetailLine node within the Document node

 

Instead of the normal repeat like so:


  

Then you can even combine variables and index like so:


  

Here we first find the total number of DetailLine nodes and repeat the sub-section this number of times and keep track of the current index number via the variable, count.

 

Both of these repeat loops creates the same output, so as you can see you can even use an xpath expression to set the index number. A related example is found below.

 

    • Related Articles

    • Node Existence & Conditions

      XPath allows you to verify the existence of nodes, check if they contain data, count occurrences, and ignore namespaces. This section covers essential techniques for validating and filtering XML elements. Calculating the sum of nodes Counting ...
    • Base64 XML node to payload

      If a resource is included as base 64 in an input XML file, then you can use this advanced utilities component to extract the resource from the input file into the payload. The base64 XML node to payload workflow component has these parameters: XPath ...
    • Remove a node from XML

      This advanced utilities workflow component can remove one or multiple nodes from an input XML, that is found in the payload of the workflow. The output is a changed payload, where the selected nodes are removed. The workflow continues processing with ...
    • Repair an XML with invalid characters in a node

      Some characters cannot be used inside the value of a node or attribute within an XML file. The characters are: Original character Escaped character " &quot; ' &apos; < &lt; > &gt; & &amp; The table above indicates, that you should use the sequence ...
    • Extract payload from sub-node in input XML

      In this example we want to extract an XML file, that is stored as a sub-node in an input XML file. This could e.g. be an XML file stored inside of a SOAP input file. This input file could have contents similar to this: <SOAP-ENV:Envelope ...