Please enable JavaScript to view this site.

InterFormNG Manual 2020

Navigation: The Designer > Introduction to 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}

 

Here is an example:

 

InterFormNG_Xpath_003

 

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 variables are replaced with the actual contents before the xpath expression is validated. This means e.g. that you e.g. can concatenate two variables (e.g. var1 and var2) in this Xpath expression:

 

'{@var1} {@var2}'

 

- If none of the variables contains the delimiter character ‘ (single quote).

If one of them contains a single quote, then you can use a double quote as delimiter:

 

“{@var1} {@var2}”

 

You decide to use either double quote or single quote to delimit a string in xpath.

 

 

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.