In InterFormNG you use the language XPath hen 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 be used with InterFormNG:
In XPath you can e.g. refer to data in this XML file:
If you type this in InterFormNG as the XPath:
/Data/Header/Type , then the actual text retrieved is: VDA4902.
Also the Xpath: /Data/Header/Type/text() will retrieve this value.
(The function, text() retrieves the value of the selected node in the XML file).
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. It the Xpath does not start with a ‘/’, then the path is relative to a an XML file cursor. This cursor can in InterFormNG previously have been set via a Select Cursor Element in InterFormNG or via a ForEach Element:
/Data/Body/Label
If you insert the XPath above in a ForEach Element, then InterFormNG will run the loop for each label node in the XML file.
You can then inside of the ForEach loop refer to the data inside the label e.g. with an Xpath like this:
Receiver/ReceiverLine[@sequence=1]
This will retrieve the value ‘Dr. Ing. H.c.F. Porsche AG’ in the label above by selecting the ReceiverLine element, where the attribute ‘sequence’ equals “1".
You can also retrieve an attribute. If you e.g. consider this addition to the type element above:
With this XPath specification you will retrieve the value of the attribute mytype of the Type element: /Data/Header/Type/@mytype
So use the @-sign to refer to attributes.
If you only specify the Xpath to a level with subelements, then all subelements are also included in the result. If you e.g. state the XPath: /Data/Header you will get this result:
VDA4902 A5 PORSCHE STANDARD wernert 1 001 1 DEFAULT DEFAULT
If you want to concatenate constants with data from the XML file you can use the concat function e.g. like this:
concat('ABCD',/Data/Header/Type,'EFGH')
This inserts the XML data right in between the constants: ‘ABCDVDA4902 EFGH’.
You may notice, that there above is a blank between the XML data and the trailing constant. You can remove that by removing leading and trailing blanks of the XML data like so:
concat('ABCD',normalize-space(/Data/Header/Type),'EFGH')
You can also substring data like this: substring(/Data/Header/Type, 2,3)
The first parameter of the substring command is the string, that should be subset, the second is the start position and the third is the length, so if /Data/Header/Type contains the string ‘VDA4902', the result will be ‘DA4'.
Other string functions are:
substring-before(expression1 ,expression2)
substring-after(expression1 ,expression2)
replace(expression1, expression2, expression3)
substring-before searches for the text, expression2 inside expression1 and returns the part of expression1, that precedes the found position. This formular returns a ‘c’:
substring-before("c:\dir", ":\")
substring-after searches for the text, expression2 inside expression1 and returns the part of the expression1, that follows the found position. This returns ‘dir’:
substring-before("c:\dir", ":\")
replace replace any occurrence of expression 2 in expression1 with expression3. So the expression replace('abc@def','@','123') will result in the string: ‘abc123def’.
The function, translate is able to translate a character with another. It will search the first string for any character in the second string and if found it will use the same number in the list from the third string instead. This will e.g. replace any comma with a dot in the variable, in:
translate(“{@in}”, ”,” , ”.”)
–S1– S2 S3
If the third string is empty the character will be replaced with nothing: (commas are removed)
translate(“{@in}”, ”,” , ””)
You can also convert from lower case to upper case in this way:
translate(“{@in}”, ”abcde” , ”ABCDE”)
If substring-before and substring-after is not able to find the text, then an empty string is returned.
You can also do normal calculations as adding, subtracting, multiplicating, dividing in XPath. This can e.g. also be stated as an XPath expression:
substring(/Data/Header/Type, 4,3)*5 div 6 (you need blanks around ‘div’)
With the value (‘VDA4902') as mentioned above (for /Data/Header/Type) this will be calculated as 490 multiplied with 5 (equal 2450) divided by 6 making the result: ‘408.3333333333333'.
So as you can see divide is stated as ‘div’ and modulo is ‘mod’.
A numeric result is converted to a string when returning the value so you can e.g. combine substring functions to the result, so we can even limit the number above to 2 decimals in this manner:
concat(substring-before(substring(/Data/Header/Type, 4,3)*5 div 6,'.') ,'.',substring(substring-after(substring(/Data/Header/Type, 4,3)*5 div 6,'.'),1,2))
Numbers must be written with a dot as decimal point without any thousand separator. To convert a decimal comma into a dot and to remove any dots (used as 1000 delimiter) you can use the translate function like this: translate(string,”,.”,”.”). Assuming you are using a dot as a decimal point and a comma as thousand separator you can remove the commas with this expression: translate(string,”,”,”“).
The result can then be used in numeric expressions.
The explaination of the above is, that we concatenate the substrings consisting of:
1) The part of the amount, that is in front of the ‘.’ i.e. ‘408' in this case.
2) The ‘.’.
3) Only the 2 first characters of the part of the amount, that is after the ‘.’ i.e. ‘33' in this case.
- making the final result: ‘408.33'.
A more simple round of can also be done if you set the data type (in the text element) to numeric and then use this formula:
round(100*(/Data/Header/Type*5 div 6)) div 100
(The round function will round off to the nearest integer).
Please note, that Xpath is as default using round half to even, when you set a fixed number of decimals on a numeric field via a mask on the text style. This is described on page .
Path expressions
Xpath also enables you to condition the path to an element. With this you can select a specific branch in the xml tree, which fits a certain condition. If you e.g. consider the intro_demo.xml file you can e.g. find the contact person for the company at a specific address in this manner:
/Root/Document[Adress1='Tulip Road 16']/Contact_person, which will return ‘Susan Sunflower’.
You can also 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
If you want to do multiple comparisons, then you could use nested IF..ELSE elements, or you could use an xpath expression, that returns either true or false and simply compare this in an IF with the boolean, true.
This xpath expression will return true, if the variable is a single numeric character:
'{@var1}'>='0' and '{@var1}'<='9'
You can also use boolean expressions to check if e.g. a string is equal to any of the strings in a list. The ‘string’ do not even need to be a string, so this xpath comparison could be used:
'{@var1}'=(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f',’A’,’B’,’C’,’D’,E’,’F’)
This returns true, if the variable, var1 contains a single hex character, which could be a character in the ranges 0-9, a-f, or A-F.
You can even use regular expressions with Xpath, if you use the matches Xpath function. This expression checks the variable, MyVariable:
matches("{@MyVariable}", "^(DIN|ISO) A[3-5]$")
The regular expression above tests, if the variable:
•starts with either 'DIN' or 'ISO'
•then a space and the letter 'A'
•finish with 3, 4 or 5
You can use the expression in an IF element to let the output depend on the result of this test.
Remember, that you can combine the XPath functions, and that there are many other functions not mentioned here. This appendix hopefully gives you an idea of how strong a tool this is. You can e.g. refer to this link for additional information: http://www.w3schools.com/xpath/xpath_functions.asp
Apart from the normal Xpath (1.0) functions, InterFormNG also supports some extra internal XPath functions and variables can also be used.