Please enable JavaScript to view this site.

InterFormNG2 Manual

Navigation: XPath

Using variables in Xpath expressions

Scroll Prev Top Next More

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 e.g. $var is not the same as $Var.

 

When you assign a value to a variable you need to type in a valid Xpath expression. That means e.g. that you can assign a numeric value to a variable without any delimiters, but if you want to assign an alphanumeric value e.g. a string, then you need to use delimiters around the string. You can use either ' or " to delimit the string.

 

Here is an example of how you can assign a variable with an Xpath function:

 

Xpath_example001

 

Here the value of variable, filename is set as value of variable, invoice_text (which in this case is language dependent and contains e.g. the value “Invoice” or “Rechnung”) concatenated with the document number, which is found in the XML file and finally the extension “.pdf” is added, so that a possible value of filename could be “Invoice1001.pdf” or ‘Rechnung1001.pdf’.

 

The example above is based on the demo xml file, that has multiple Document nodes, each with a DocumentNo, so this expression is not valid:

concat($invoice_text,/Root/Document/DocumentNo,'.pdf')

as the reference: /Root/Document/DocumentNo actually returns a list of 4 values.

 

So you need to select only one value as above. With Document[1] we refer to the first occurrence of the Document node.

 

 

If a node in your input XML file contains a text containing both a single quote and a double qoute, then you need to replace these values with two other characters, that you are sure, that will NEVER appear in the node. If you e.g. consider a node containing this text:

 

"Mary's lam had a littl' lam" (including the double quotes).

 

Then you need to convert one of the characters into another unique character if you e.g. want to insert this into a variable and later use it in xpath expressions. One way could be this:

 

var1= translate(/Root/Node,'"','#')

 

With this Xpath expression the sentence has been converted into:

#Mary's lam had a littl' lam#

 

Now you can do a new translation:

 

var1=translate($var1,"'",'£')

 

With the last translation we have now also converted any quote into £:

 

#Mary£s lam had a littl£ lam#

 

Now you can translate back when you use the variable, var1 in an expression like below:

 

concat($Start,' ',translate(translate($test,'#','"'),"£","'"))

 

If the variable, start contained ‘This is the sentence:’, then the result is:

 

This is the sentence: "Mary's lam had a littl' lam"

 

Another simple way could also be to output the texts in a flow area without the need to use concat.