Node Existence & Conditions

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 specific nodes

Verifying if a node exists

Checking if a node is not blank

Checking if a node is blank

Using attributes of a node

Ignoring namespaces in XPath expressions


Calculating the sum of nodes


If you want to output the sum of all detail lines of a document, then the best solution is to output a sum, that is included in the input file, but if that sum is not included, then you can either create a repeat loop in the designer and sum up the values in a variable, but you can also simply use the Xpath sum function like to:

sum(/Root/DetailLine/Amount)




Counting specific nodes

If you want to know how many nodes there are in an input XML file, then you can use the XPath function, count. The count function simply counts the number of nodes with the specified path. This function counts the number of detail lines in an XML file:

count(/Root/Document/DetailLine)


Verifying if a node exists

If you want to verify, if a node is empty, then you can use this comparison: (MyNode=''), but this only returns true, if the node, MyNode actually exists and is empty. If you use the same comparison for a node, that does not exist, then this comparison is false (unlike in InterFormNG1, which use XPath version 1). For comparisons like that there is in a "Checking if a node is not blank" topic.

 

So if you want to insert a condition, that can verify if the node exists, then you need to use another condition.

One condition could be this:
count(MyNode)>0
or
boolean(MyNode)

 

In regards to performance the test boolean(MyNode) is faster, but perhaps not that clear for a new user.

 

So if you in InterFormNG2 wants to insert a condition to verify if the node, MyNode is either blank (null) or does not exist, then you could use this condition to combine them:

MyNode='' or boolean(MyNode)=false()
or
MyNode='' or not (boolean(MyNode))

 

An alternative is to consider the ng:compare function.

Checking if a node is not blank

If you want to verify, if a node is not blank, then you might consider to use the comparision: MyNode!=''. This condition is however true in two scenarios: If the node, MyNode exists and the contents of the node is not empty/null, but this is expression is also true, if the node, MyNode does not exist. So if you want to verify, that there is data in the node, then you need to change the condition.

 

The most simple way is to do a string compare i.e. convert the contents of the node into a string and then compare the result. In this way you could substitute the condition MyNode!='' with the condition string(MyNode)!=''. This works by converting the node selection (which can be no nodes, one node or multiple nodes) into a string and the compare the string with an empty string. Please note, that the string function is a standard XPath function, that must be written in lower case.

 

Other solutions could be to use two tests to verify, that the node actually exists and also that the value of the node is not empty/null. You can do that with this condition:

Mynode!=''
and
boolean(MyNode)

 

another expression to do the same is:

Mynode!=''
and
count(MyNode)>0


An alternative is to consider the ng:compare function.

Checking if a node is blank

If you want to verify, if a node is blank, then you might use the comparision: MyNode=''. This condition is however false in two scenarios: If the node, MyNode exists and the contents of the node is not empty/null, but this is expression is also false, if the node, MyNode does not exist.

 

The most simple way is to do a string compare i.e. convert the contents of the node into a string and then compare the result. In this way you could substitute the condition MyNode!='' with the condition string(MyNode)=''. This works by converting the node selection (which can be no nodes, one node or multiple nodes) into a string and the compare the string with an empty string. Please note, that the string function is a standard XPath function, that must be written in lower case.

 

Other alternatives are here:

If you really want to test, if the node does not contains data, then you should use a condition like this: 

MyNode=''
or
not(boolean(MyNode))
 
or  
Mynode=''
or
or count(MyNode)=0

You can refer to examples in the sections: "Verify if a node exists" and "Verify if a node is not blank" for other examples how to test on the contents of a node, while being able to handle non-existing nodes. An alternative is to consider the ng:compare function.

Using attributes of a node

If you consider this sample XML file:

<persons>
  <chairman gender="female">
    <name>Maggie Frederiksen</name>
  </chairman>
  <person gender="female">
    <name>The Little Mermaid</name>
  </person>
  <person gender="male">
    <name>Mr. Andersson</name>
  </person>
  <person gender="male">
    <name>Mr. Nilsson</name>
  </person>
  <person gender="female">
    <name>Mercedes Hansen</name>
  </person>
  <person gender="female">
    <name>Lada Jaskagidaj</name>
  </person>
  <person gender="N/A">
    <name>Abcd Jensen</name>
  </person>
</persons><?xml version="1.0" encoding="UTF-8"?> 

 

Here the gender is defined as an attribute of the chairman and person nodes.

In the designer the attributes in XML files are shown with a preceding @-sign as below:

  NG2XPathIntro0001

 

If you want to refer to the gender of the chairman, then you can in the designer simply drag the attribute to the result to map it.

The Text element will look like this:

  


If you want to count all the male persons, then you can that like this:

count(/persons/person[@gender='male'])
Ignoring namespaces in XPath expressions
When you refer to the contents of the input XML file, then you will as default always include the namespace in the reference like e.g.
/Root/nmspc:myNode

Where nmspc is the name of the namespace.

 

If you want to make this work no matter what the namespace is called, then you can replace the namespace with an asterix like so:

/Root/*:myNode

You can use a local-name notation, if there are multiple namespaces with the same node:

/Root/*[local-name() = 'myNode']
 

    Notice: Help Center Transition Update

    As of January 13, 2025, we are excited to announce that our new Help Center is in the final stages of development. While the Knowledge Base is already accessible, our current JIRA system will continue to manage support tickets during this transition period. For assistance with InterForm Output Management Software, please refer to the Support for InterForm Output Management Software.

    We appreciate your patience and understanding as we work to enhance your support experience. If you have any questions or encounter any issues, please do not hesitate to reach out via the existing support channels.

    Best regards,
    The InterForm Support Team


      • Related Articles

      • 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 ...
      • Advanced XPath Features

        For more complex scenarios, XPath provides regular expressions, variable assignments, and special functions to manipulate and evaluate XML data dynamically. This section covers these advanced features. Working with regular expressions in XPath Using ...
      • XPath

        In InterFormNG2, you use the language XPath for referencing data from the XML file. There are many sources of information if you want to learn more about XPath, but you can also simply read the few examples below to get a good idea of how it can be ...
      • Using variables in Xpath expressions

        XPath is a strong tool in itself, but you can even use variables in your XPath expressions. The way it works is by referring to a variable as: $variable You use the variable element to assign a value to a variable. Variables are case-sensitive, so ...
      • Iteration & Grouping

        When working with repeated data, XPath enables you to loop through multiple elements or extract specific subsets of data based on conditions. This section covers repeat loops, grouping techniques, and handling warnings when no nodes are selected. ...