Xpath Requests
Reminder:
- XPath is used to navigate through elements and attributes in an XML document:
- It is a syntax for defining parts of an XML document
- It uses path expressions to navigate in XML documents
- It contains a library of standard functions
- XPATH request allows to retrieve in the spreadsheet columns, information coming from the XML flows generated by the Configurator and Catalog modules
- XPATH request can only be associated with the property “Entry Mode” of the columns in the spreadsheet
- XPATH request has to target a single value. XPATH request that targets multiple values will cause an error and prevent the reload of configuration.information: XPATH requests are directly entered in the corresponding property pop-up.
XML coming from the Configurator module
- For the parent line (of type CP7), the XML source contains the full XML returned by the configuration process.
- For the child sublines (of type SI7, CP7), the XML source only contains:
- The
<cam:ItemSalesBreakdownLine>sub-tree corresponding to the current CP7 or LNK subline +The<cam:ProductDictionary>sub-tree
- The
XML coming from the Catalog module
- For the parent line (of type CT7), the XML source contains the full XML returned by the Catalog.
- For the child sublines (of type LNKCT), the XML source only contains:
- The
<cat:RelatedProductLink>sub-tree corresponding to the current LNKCT subline +The<cat:ProductDictionary>sub-tree
- The
UNDERSTANDING THE SYNTAX:
Definition:
These definitions refer to the code block below.
- XML documents are treated as trees of nodes.
<cartLines>is the rootNode<ListPrice>310</ListPrice>is an element node- type="CT7" is an attribute node
- 310 is an atomic node
<cartLines>is the parent of<cartLine><ListPrice>, <NetUnitPric>are children of<carLine>- Nodes having the same parent are siblings
- A node's parent, parent's parent, etc. is an ancestor
- A node's child, child's child, etc. is a descendant
<?xml version="1.0" encoding="utf-8"?>
<cartLines>
<cartLine type="CT7" >
<CatalogPrice>346</CatalogPrice>
<ListPrice>310</ListPrice>
<NetUnitPrice>300.76</NetUnitPrice>
<NetTotalPrice>300.76</NetTotalPrice>
<TaxCode>510</TaxCode>
<TaxAmount>0</TaxAmount>
<ItemDescr>Gas Boiler Combi 75</ItemDescr>
</cartLine>
</cartLines>
Nodes/Attributes Selection
| SYNTAX | EXAMPLE | EXPLANATION |
|---|---|---|
| Nodename | Form | Selects all nodes with the name "nodename" |
| SYNTAX | EXAMPLE | EXPLANATION |
|---|---|---|
| / | / | origin Selects from the root node Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! |
| parentNode/childNode | cartLines/cartLine | Selects all node elements that are descendants of a parent |
| // | //cartLine | Selects nodes in the document from the current node that match the selection no matter where they are |
| parentNode//childNode | cartLines//cartLine | Selects all node elements that are descendant of the parent element, no matter where they are under the parent element |
| . | Selects the current node | |
| .. | Selects the parent of the current node | |
| @ | //@lang | Selects attributes |
Predicates
Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets.
| SYNTAX | EXAMPLE | EXPLANATION |
|---|---|---|
| [<predicat>] | /cartLines/cartLine[1] | Selects the first cartLine element that is the child of the cartLines element. |
| SYNTAX | EXAMPLE | EXPLANATION |
|---|---|---|
| [<predicat>] | //cartLine[@type] | Selects all the cartLine elements that have an attribute named type |
| [<predicat>] | //cartLine[@type='CP7'] | Selects all the cartLine elements that have an attribute named type with a value of ‘CP7’ |
FOCUSING ON FUNCTIONS:
Accessory Functions
| FUNCTIONS | EXPLANATION |
|---|---|
| node-name(node) | Returns the node-name of the argument node |
| nilled(node) | Returns a Boolean value indicating whether the argument node is nilled |
| data(item.item ...) | Takes a sequence of items and returns a sequence of atomic values |
| base-uri() base-uri(node) | Returns the value of the base-uri property of the current or specified node |
| document-uri(node) | Returns the value of the document-uri property for the specified node |
Error and Trace Functions
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| error() error(error) error(error,description) error(error,description,error-object) | error(QName('http://example.com/test', 'err:toohigh'), 'Error: Price is too high') | Result: Returns http://example.com/test#toohigh and the string "Error: Price is too high" to the external processing environment |
| trace(value,label) | Used to debug queries |
Functions on Numeric Values
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| number(arg) | number('100') - Result: 100 | Returns the numeric value of the argument. The argument could be a boolean, string, or nodeset |
| abs(num) | abs(3.14) - Result: 3.14 abs(-3.14) - Result: 3.14 | Returns the absolute value of the argument |
| ceiling(num) | Example: ceiling(3.14) - Result: 4 | Returns the smallest integer that is greater than the number argument |
| floor(num) | floor(3.14) - Result: 3 | Returns the largest integer that is not greater than the number argument |
| round(num) | round(3.14) - Result: 3 | Rounds the number argument to the nearest integer |
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| round-half-to-even() | round-half-to-even(0.5) - Result: 0 round-half-to-even(1.5) - Result: 1 |
Functions on Strings
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| string(arg) | string(314) - Result: "314" | Returns the string value of the argument. The argument could be a number, boolean, or nodeset |
| codepoints-to-string(int,int,...) | codepoints-to-string(84, 104, 233, 114, 232, 115, 101) - Result: 'Thérèse' | Returns a string from a sequence of code points |
| string-to-codepoints(string) | string-to-codepoints("Thérèse") Result: 84, 104, 233, 114, 232, 115, 101 | Returns a sequence of code points from a string |
| codepoint-equal(comp1,comp2) | Returns true if the value of comp1 is equal to the value of comp2, according to the Unicode code point collation (http://www.w3.org/2005/02/xpathfunctions/collation/codepoint), otherwise it returns false | |
| compare(comp1,comp2) compare(comp1,comp2,collation) | compare('ghi', 'ghi') - Result: 0 | Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to comp2, or 1 if comp1 is greater than comp2 (according to the rules of the collation that is used) |
| concat(string,string,...) | concat('XPath ','is ','FUN!') Result: 'XPath is FUN!' | Returns the concatenation of the strings |
| string-join((string,string,...),sep) | string-join(('We', 'are', 'having', 'fun!'), ' ') Result: 'We are having fun!' string-join(('We', 'are', 'having', 'fun!')) Result: 'Wearehavingfun!' string-join((), 'sep') - Result: '' | Returns a string created by concatenating the string arguments and using the sep argument as the separator |
| substring(string,start,len) substring(string,start) | substring('Beatles',1,4) - Result: 'Beat' substring('Beatles',2) - Result: 'eatles' | Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end |
| string-length(string) string-length() | string-length('Beatles') - Result: 7 | Returns the length of the specified string. If there is no string argument it returns the length of the string value of the current node |
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| normalize-space(string) normalize-space() | normalize-space(' The XML ') - Result: 'The XML' | Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. If there is no string argument it does the same on the current node |
| normalize-unicode() | ||
| upper-case(string) | upper-case('The XML') - Result: 'THE XML' | Converts the string argument to upper-case |
| lower-case(string) | lower-case('The XML') - Result: 'the xml' | Converts the string argument to lower-case |
| translate(string1,string2,string3) | translate('12:30','30','45') - Result: '12:45' translate('12:30','03','54') - Result: '12:45' translate('12:30','0123','abcd') - Result: 'bc:da' | Converts string1 by replacing the characters in string2 with the characters in string3 |
| contains(string1,string2) | contains('XML','XM') - Result: true | Returns true if string1 contains string2, otherwise it returns false |
| starts-with(string1,string2) | starts-with('XML','X') - Result: true | Returns true if string1 starts with string2, otherwise it returns false |
| ends-with(string1,string2) | ends-with('XML','X') - Result: false | Returns true if string1 ends with string2, otherwise it returns false |
| substring-before(string1,string2) | substring-before('12/10','/') - Result: '12' | Returns the start of string1 before string2 occurs in it |
| substring-after(string1,string2) | substring-after('12/10','/') - Result: '10' | Returns the remainder of string1 after string2 occurs in it |
| matches(string,pattern) | matches("Merano", "ran") - Result: true | Returns true if the string argument matches the pattern, otherwise, it returns false |
| replace(string,pattern,replace) | replace("Bella Italia", "l", "*") - Result: 'Be**a Ita*ia' replace("Bella Italia", "l", "") - Result: 'Bea Itaia' | Returns a string that is created by replacing the given pattern with the replace argument |
| tokenize(string,pattern) | tokenize("XPath is fun", "\s+") Result: ("XPath", "is", "fun") |
Functions on Boolean Values
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| boolean(arg) | Returns a boolean value for a number, string, or node-set | |
| not(arg) | not(true()) - Result: false | The argument is first reduced to a boolean value by applying the boolean() function. Returns true if the boolean value is false, and false if the boolean value is true |
| true() | true() - Result: true | Returns the boolean value true |
| false() | false() - Result: false | Returns the boolean value false |
Functions on Nodes
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| name() name(nodeset) | Returns the name of the current node or the first node in the specified node set | |
| local-name() local-name(nodeset) | Returns the name of the current node or the first node in the specified node set - without the namespace prefix | |
| namespace-uri() namespace-uri(nodeset) | Returns the namespace URI of the current node or the first node in the specified node set |
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| lang(lang) | Lang("en") is true for <p xml:lang="en">...</p> Lang("de") is false for <p xml:lang="en">...</p> | Returns true if the language of the current node matches the language of the specified language |
| root() root(node) | Returns the root of the tree to which the current node or the specified belongs. This will usually be a document node |
Functions on Sequences
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| index-of((item,item,...),searchitem) | index-of ((15, 40, 25, 40, 10), 40) - Result: (2, 4) index-of (("a", "dog", "and", "a", "duck"), "a") - Result (1, 4) index-of ((15, 40, 25, 40, 10), 18) - Result: () | Returns the positions within the sequence of items that are equal to the searchitem argument |
| remove((item,item,...),position) | remove(("ab", "cd", "ef"), - Result: ("ab", "cd", "ef") remove(("ab", "cd", "ef"), - Result: ("cd", "ef") remove(("ab", "cd", "ef"), 4) - Result: ("ab", "cd", "ef") | Returns a new sequence constructed from the value of the item arguments, with the item specified by the position argument removed |
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| empty(item,item,...) | empty(remove(("ab", "cd"), 1)) - Result: false | Returns true if the value of the arguments IS an empty sequence, otherwise it returns false |
| exists(item,item,...) | exists(remove(("ab"), 1)) - Result: false | Returns true if the value of the arguments IS NOT an empty sequence, otherwise it returns false |
| distinctvalues((item,item,...),collation) | distinct-values((1, 2, 3, 1, 2)) - Result: (1, 2, 3) | Returns only distinct (different) values |
| insertbefore((item,item,...),pos,inserts) | insert-before(("ab", "cd"), 0, "gh") Result: ("gh", "ab", "cd") insert-before(("ab", "cd"), 1, "gh") Result: ("gh", "ab", "cd") insert-before(("ab", "cd"), 2, "gh") Result: ("ab", "gh", "cd") insert-before(("ab", "cd"), 5, "gh") Result: ("ab", "cd", "gh") | Returns a new sequence constructed from the value of the item arguments - with the value of the inserts argument inserted in the position specified by the pos argument |
| reverse((item,item,...)) | reverse(("ab", "cd", "ef")) - Result: ("ef", "cd", "ab") reverse(("ab")) - Result: ("ab") | Returns the reversed order of the items specified |
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| subsequence((item,item,...),start,len) | subsequence(($item1, $item2, $item3,...), 3) Result: ($item3, ...) subsequence(($item1, $item2, $item3, ...), 2, 2) Result: ($item2, $item3) | Returns a sequence of items from the position specified by the start argument and continuing for the number of items specified by the len argument. The first item is located at position 1 |
| unordered((item,item,...)) | Returns the items in an implementation dependent order | |
| zero-or-one(item,item,...) | Returns the argument if it contains zero or one items, otherwise it raises an error | |
| one-or-more(item,item,...) | Returns the argument if it contains one or more items, otherwise it raises an error | |
| exactly-one(item,item,...) | Returns the argument if it contains exactly one item, otherwise it raises an error |
Aggregate Functions
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| count((item,item,...)) | Returns the count of nodes | |
| avg((arg,arg,...)) | avg((1,2,3)) - Result: 2 | Returns the average of the argument values |
| max((arg,arg,...)) | max((1,2,3)) - Result: 3 max(('a', 'k')) - Result: 'k' | Returns the argument that is greater than the others |
| min((arg,arg,...)) | min((1,2,3)) - Result: 1 min(('a', 'k')) - Result: 'a' | Returns the argument that is less than the others sum(arg,arg,...) Returns the sum of the numeric value of each node in the specified node-set |
Generate Sequences
| FUNCTIONS | EXPLANATION |
|---|---|
| id((string,string,...),node) | Returns a sequence of element nodes that have an ID value equal to the value of one or more of the values specified in the string argument |
| idref((string,string,...),node) | Returns a sequence of element or attribute nodes that have an IDREF value equal to the value of one or more of the values specified in the string argument |
| doc(URI) doc-available(URI) | Returns true if the doc() function returns a document node, otherwise it returns false |
| collection() collection(string) |
Context Functions
| FUNCTIONS | EXAMPLE | EXPLANATION |
|---|---|---|
| position() | //book[position()<=3] Result: Selects the first three book elements | Returns the index position of the node that is currently being processed |
| last() | //book[last()] - Result: Selects the last book element | Returns the number of items in the processed node list |
| current-dateTime() | Returns the current dateTime (with timezone) | |
| current-date() | Returns the current date (with timezone) | |
| current-time() | Returns the current time (with timezone) | |
| implicit-timezone() | Returns the value of the implicit timezone | |
| default-collation() | Returns the value of the default collation | |
| static-base-uri() | Returns the value of the base-uri |
