Using variables in Xpath expressions

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 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.

    • Related Articles

    • XPath

      In InterFormNG2 you use the language XPath for referencing to data from the XML file. There are many sources of information, if you want to know more about XPath, but you can also simply read the few examples below to have a good idea of how it can ...
    • Variables

      In InterFormNG2 there are 4 kind of variables: 1.User defined variables, that you define in the designer. 2.Workflow variables. They can be defined in the workflow either by setting a single variable or multiple variables at a time. 3.Predefined ...
    • Excel variables

      You can use variables in the Excel designer exactly like the variable element in the normal template designer. These variables can be used in any Xpath expression. You can create a new variable or update the value of a variable in two ways: 1.You can ...
    • Set multiple workflow variables

      The advanced set meta data workflow component, Set multiple workflow variables, sets/defines multiple workflow variables. This can be used later in the workflow and also in a template. The variables can be referenced in XPath expressions with a ...
    • XPath wizard

      When you in InterFormNG2 insert a value in a field, then you for most have these options: 1.You can insert a fixed text. 2.You can type a dynamic XPath expression where you need to remember each XPath function and the matching parameters. 3.For some ...