XPath

Xpath content
Mirjana's picture

Conditional XPath

Example of condition in XPath that can be used instead of bpml condition. Much shorter and simpler.

<assign to="result" from="if(urgent_ind/text() = 1,'U','N')"></assign>

 

Mirjana's picture

Xpath - Check filename attribute after FSA multiple collect

Request:
 
Write the rule that will check if the filename has an extension .inv or .sls, and depending on that will be extracted in a separate folders.
 
The Process Data:
 
<ProcessData>
         <BPID>5060827</BPID>
         <currentTimeMillis>1288382751118</currentTimeMillis>
         <time>20101029160511851</time>
         <loop_counter>1</loop_counter>
         <X12_ArchiveDirectory>/app/archive/</X12_ArchiveDirectory>
         <FSA_Document1 SCIObjectID="serverName:38a97b0b:12bf942739e:1144" filename="ConcatSLS2.sls"/>
         <FSA_Document2 SCIObjectID="" filename="ConcatINV2.inv"/>
         <FSA_DocumentCount>2</FSA_DocumentCount>
         <Prev_NotSuccess_Adv_Status/>
         <INVOKE_ID_LIST>5060828</INVOKE_ID_LIST>
</ProcessData>
 
 
The wrong XPath used in the rule is:
contains(string('FSA_Document',//loop_counter/text(),' ',SCIObjectID/text()), string('.sls')
 
… and it does not work!!!
 
To check if the filename attribute in FSA_Document[n] element contains ‘inv’ or ‘sls’, XPath should be like this:
 
contains(//*[name()=concat('FSA_Document',//loop_counter/text())]/@filename,'.sls')
 
That would be interpreted as:
 
… check if the attribute ’filename’ (@filename)
… inside of the element which name is FSA_Document[n] (composed dynamically by concat function)
… contains ‘.sls’ string
 
Result will be boolean, true or false, and depending on that FSA extraction folder can be set .
 
admin's picture

XPath - Number Functions Examples

The part of XML document that we will use for the following examples is:
 
..........
<items>
         <item>
                   <productID description="Product_1">CO11</productID>
                   <quantity>5</quantity>
                   <price>1.23</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_2">CO12</productID>
                   <quantity>7</quantity>
                  <price>2.34</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_3">CO13</productID>
                   <quantity>9</quantity>
                   <price>3.45</price>
                   <currency>USD</currency>
         </item>
</items>
         <total>53.58</total>
..........
 
1.  sum(Data/instanceData/POdocument/items/item/price)
 
Result: 7.02
 
2.  //item[2]/price * //item[2]/quantity
 
Result: 16.38    
 
3.  round(//items/item[1]/price)
 
Result: 1
 
4.  floor(//items/item[1]/price)
 
Result: 1
 
5.  ceiling(//items/item[1]/price)
 
Result: 2
 
6.  (//items/item[1]/price*//items/item[1]/quantity) + (//items/item[2]/price*//items/item[2]/quantity) + (//items/item[3]/price*//items/item[3]/quantity)
 
Result: 53.58
 
7.  (//items/item[1]/price*//items/item[1]/quantity) + (//items/item[2]/price*//items/item[2]/quantity) + (//items/item[3]/price*//items/item[3]/quantity) = //total
 
Result: true
admin's picture

XPath - Boolean Functions Examples

Function boolean converts its argument to a boolean as follows:
  • a number is true if and only if it is neither positive or negative zero nor NaN
  • a node-set is true if and only if it is non-empty
  • a string is true if and only if its length is non-zero
  • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type                 
1.  boolean(//perm[position()=5]/@id)
 
..........
<perm type="admin" id="1">POReqSend</perm>
<perm type="cert" id="cert_1">CA_CERTS</perm>
<perm type="cert" id="cert_2">SYSTEM_CERTS</perm>
<perm type="template">POConfirmSend.xfm</perm>
-->  <perm type="admin" id="2">POQueryApprover</perm>
<perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
<perm>permission_2</perm>
            ..........
 
As value of id attribute of the fifth perm element is a number, boolean function will return true.
 
Result: true
 
2. boolean(Data/username/text())
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
            ..........
 
As content of element username exist and it is a string, boolean function will return true.
 
Result: true
 
3. boolean(Data/PrimaryDocument/text())
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
            ..........
 
As element PrimaryDocument does not contain any content, boolean function will return false.
 
Result: false
 
4.  not(Data[username/text() = 'Joe'])
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
            ..........
 
Result: false
 
5. not(Data[username/text() = 'Joe_123'])
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
            ..........
 
Result: true

 

admin's picture

XPath - String Functions Examples

The part of XML document that we will use for the following examples is:
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
..........
<firstname>Joe</firstname>
<lastname>User</lastname>
<fullname>Joe User</fullname>
<email>Joe.User@gmail.com</email>
..........
 
1.  concat(//firstname/text(),'_',//lastname/text())
 
Concatenate content of firstname element, string '_' and content of lastname element.
 
Result: Joe_User
 
2.  starts-with(/*/*[position()=2]/text(),'J')
 
Check if the content of the child of the root element, at the second position, starts with the letter 'J'.
 
Result: true
 
3. starts-with(//username,'J')
 
Check if the content of username element starts with the letter 'J'.
 
Result: true
 
4.  starts-with(//username,'o')
 
Check if the content of username element starts with the letter 'o'.
 
Result: false
 
5.  contains(//username,'o')
 
Check if the content of username element contains the letter 'o'.
 
Result: true
 
6.  substring-before(//email,'@')
 
Select a substring of email element, returns start of email element before string '@' that occurs in it
 
Result: Joe.User
 
7.  substring-after(//email,'@')
 
Select a substring of email element, returns remainder of email element after string '@' that occurs in it
 
Result: gmail.com
 
8.  substring(//email,9,1)
 
Select a substring starting at the position 9 with length 1.
 
Result: @
 
9.  string-length(//email)
 
Return the number of characters in the string that is content of email element.
 
Result: 8
 
10.                   translate(//email,'@','_')
 
Translates the character '@' with '_' in the element email.
 
Result: Joe.User_gmail.com
 
 
admin's picture

XPath - String Functions

The part of XML document that we will use for the following examples is:
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
..........
<firstname>Joe</firstname>
<lastname>User</lastname>
<fullname>Joe User</fullname>
<email>Joe.User@gmail.com</email>
..........
 
1.  concat(//firstname/text(),'_',//lastname/text())
 
Concatenate content of firstname element, string '_' and content of lastname element.
 
Result: Joe_User
 
2.  starts-with(/*/*[position()=2]/text(),'J')
 
Check if the content of the child of the root element, at the second position, starts with the letter 'J'.
 
Result: true
 
3. starts-with(//username,'J')
 
Check if the content of username element starts with the letter 'J'.
 
Result: true
 
4.  starts-with(//username,'o')
 
Check if the content of username element starts with the letter 'o'.
 
Result: false
 
5.  contains(//username,'o')
 
Check if the content of username element contains the letter 'o'.
 
Result: true
 
6.  substring-before(//email,'@')
 
Select a substring of email element, returns start of email element before string '@' that occurs in it
 
Result: Joe.User
 
7.  substring-after(//email,'@')
 
Select a substring of email element, returns remainder of email element after string '@' that occurs in it
 
Result: gmail.com
 
8.  substring(//email,9,1)
 
Select a substring starting at the position 9 with length 1.
 
Result: @
 
9.  string-length(//email)
 
Return the number of characters in the string that is content of email element.
 
Result: 8
 
10.                   translate(//email,'@','_')
 
Translates the character '@' with '_' in the element email.
 
Result: Joe.User_gmail.com
 
admin's picture

XPath - Examples with Operators

 

Operator
Description
 |
Computes two node-sets
+
Addition
-
Subtraction
*
Multiplication
div
Division
=
Equal
!=
Not equal
Less than
<=
Less than or equal to
Greater than
>=
Greater than or equal to
or
or
and
and
mod
Modulus (division remainder)

The part of XML document that we will use for the following examples is:
 
..........
<items>
         <item>
                   <productID description="Product_1">CO11</productID>
                   <quantity>5</quantity>
                   <price>1.23</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_2">CO12</productID>
                   <quantity>7</quantity>
                   <price>2.34</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_3">CO13</productID>
                   <quantity>9</quantity>
                   <price>3.45</price>
                   <currency>USD</currency>
         </item>
</items>
         <total>53.58</total>
..........
 
1.  //item/price | //item/quantity
 
Result:
 
..........
<items>
         <item>
                   <productID description="Product_1">CO11</productID>
                   <quantity>5</quantity>
                   <price>1.23</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_2">CO12</productID>
                   <quantity>7</quantity>
                   <price>2.34</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_3">CO13</productID>
                   <quantity>9</quantity>
                   <price>3.45</price>
                   <currency>USD</currency>
         </item>
</items>
         <total>53.58</total>
..........
 
 
2. //item[position() > 1]
 
Result:
 
..........
<items>
         <item>
                   <productID description="Product_1">CO11</productID>
                   <quantity>5</quantity>
                   <price>1.23</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_2">CO12</productID>
                   <quantity>7</quantity>
                   <price>2.34</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_3">CO13</productID>
                   <quantity>9</quantity>
                   <price>3.45</price>
                   <currency>USD</currency>
         </item>
</items>
         <total>53.58</total>
..........
 
3.  //item[price <= 2]/price
 
Result:
 
..........
<items>
         <item>
                   <productID description="Product_1">CO11</productID>
                   <quantity>5</quantity>
                   <price>1.23</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_2">CO12</productID>
                   <quantity>7</quantity>
                   <price>2.34</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_3">CO13</productID>
                   <quantity>9</quantity>
                   <price>3.45</price>
                   <currency>USD</currency>
         </item>
</items>
         <total>53.58</total>
..........
 
4.  //item[quantity > 6/productID/@description]
 
..........
<items>
         <item>
                   <productID description="Product_1">CO11</productID>
                   <quantity>5</quantity>
                   <price>1.23</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productIDdescription="Product_2">CO12</productID>
                   <quantity>7</quantity>
                   <price>2.34</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productIDdescription="Product_3">CO13</productID>
                   <quantity>9</quantity>
                   <price>3.45</price>
                   <currency>USD</currency>
         </item>
</items>
         <total>53.58</total>
..........
 
5. //item[price < 3 and quantity > 5]/productID/text()
 
Result:
 
..........
<items>
         <item>
                   <productID description="Product_1">CO11</productID>
                   <quantity>5</quantity>
                   <price>1.23</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_2">CO12</productID>
                   <quantity>7</quantity>
                   <price>2.34</price>
                   <currency>USD</currency>
         </item>
         <item>
                   <productID description="Product_3">CO13</productID>
                   <quantity>9</quantity>
                   <price>3.45</price>
                   <currency>USD</currency>
         </item>
</items>
         <total>53.58</total>
..........
 

 

 
admin's picture

XPath - Node Set and Predicates Examples

Predicates are always embedded in square brackets. That must be satisfied before the preceding node will be matched by an Xpath.
 
Expresion in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection.
 
1. /Data/instanceData/user_info/UserService/getUserToken/usertoken/permissions/perm[1]
 
Select the first perm child of element /Data/instanceData/user_info/UserService/getUserToken/usertoken/permissions
 
Result:
 
..........
<usertoken>
<username>admin</username>
         <firstname>Joe</firstname>
         <lastname>User</lastname>
         <fullname>Joe User</fullname>
         <email>Joe.User@gmail.com</email>
         <parentid>John</parentid>
         <groups attName="test_value">groups_content
                   <group>tpadmin</group>
                   <group>operator</group>
         </groups>
         <permissions>
                   <perm type="admin">PurchaseReqSend</perm>
                   <perm type="cert">CA_CERTS</perm>
                   <perm type="cert">SYSTEM_CERTS</perm>
                   <perm type="template">ConfSend.xfm</perm>
                   <perm type="admin">POQueryApprover</perm>
                   <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
                   <perm>permission_2</perm>
         </permissions>
</usertoken>
..........
 
perm[1] is abbreviated syntax, and full syntax would be perm[position()=1].
 
2. /Data/instanceData/user_info/UserService/getUserToken/usertoken/permissions/perm[last()]
 
Select the last perm child of element /Data/instanceData/user_info/UserService/getUserToken/usertoken/permissions
 
Result:
 
..........
<usertoken>
<username>admin</username>
         <firstname>Joe</firstname>
         <lastname>User</lastname>
         <fullname>Joe User</fullname>
         <email>Joe.User@gmail.com</email>
         <parentid>John</parentid>
         <groups attName="test_value">groups_content
                   <group>tpadmin</group>
                   <group>operator</group>
         </groups>
         <permissions>
                   <perm type="admin">PurchaseReqSend</perm>
                   <perm type="cert">CA_CERTS</perm>
                   <perm type="cert">SYSTEM_CERTS</perm>
                   <perm type="template">ConfSend.xfm</perm>
                   <perm type="admin">POQueryApprover</perm>
                   <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
                   <perm>permission_2</perm>
         </permissions>
</usertoken>
..........
 
Attributes are specified by @ prefix
 
3. //@type
 
Select all attributes @type
 
Result:
 
..........
<permissions>
         <perm type="admin"     id="1">POReqSend</perm>
         <perm type="cert"         id="cert_1">CA_CERTS</perm>
         <perm type="cert"         id="cert_2">SYSTEM_CERTS</perm>
         <perm type="template">POConfirmSend.xfm</perm>
         <perm type="admin"    id="2">POQueryApprover</perm>
         <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
         <perm>permission_2</perm>
</permissions>
..........
 
4.  //perm[@id]
 
Select perm elements which have attribute id
 
Result:
 
..........
<permissions>
         <permtype="admin"       id="1">POReqSend</perm>
         <perm type="cert"          id="cert_1">CA_CERTS</perm>
         <perm type="cert"          id="cert_2">SYSTEM_CERTS</perm>
         <perm type="template">POConfirmSend.xfm</perm>
         <perm type="admin"       id="2">POQueryApprover</perm>
         <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
<perm>permission_2</perm>
</permissions>
..........
 
5.  //perm[@*]
 
Select perm elements which have any attribute
 
Result:
 
..........
<permissions>
         <perm type="admin"       id="1">POReqSend</perm>
         <perm type="cert"          id="cert_1">CA_CERTS</perm>
         <perm type="cert"          id="cert_2">SYSTEM_CERTS</perm>
         <perm type="template">POConfirmSend.xfm</perm>
         <perm type="admin"       id="2">POQueryApprover</perm>
         <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
<perm>permission_2</perm>
</permissions>
..........
 
6.  //perm[not(@*)]
 
Select perm elements without an attribute
 
Result:
 
<permissions>
         <perm type="adminid="1">POReqSend</perm>
         <perm type="cert"    id="cert_1">CA_CERTS</perm>
         <perm type="cert"    id="cert_2">SYSTEM_CERTS</perm>
         <perm type="template">POConfirmSend.xfm</perm>
         <perm type="adminid="2">POQueryApprover</perm>
         <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
<perm>permission_2</perm>
</permissions>
 
Values of attributes can be used as selection criteria
 
7. //perm[@type='admin']
 
Select perm elements with attributes type which value is equal to 'admin'
 
Result:
 
..........
<permissions>
         <perm type="admin"       id="1">POReqSend</perm>
         <perm type="cert"    id="cert_1">CA_CERTS</perm>
         <perm type="cert"    id="cert_2">SYSTEM_CERTS</perm>
         <perm type="template">POConfirmSend.xfm</perm>
         <perm type="admin"       id="2">POQueryApprover</perm>
         <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
<perm>permission_2</perm>
</permissions>
..........
 
Function count() counts the number of selected elements
 
8. //*count(perm)=8]
 
Select elements which have eight children perm
 
Result:
 
..........
<permissions>
         <perm type="adminid="1">POReqSend</perm>
         <perm type="cert"    id="cert_1">CA_CERTS</perm>
         <perm type="cert"    id="cert_2">SYSTEM_CERTS</perm>
         <perm type="template">POConfirmSend.xfm</perm>
         <perm type="adminid="2">POQueryApprover</perm>
         <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
<perm>permission_2</perm>
</permissions>
..........
 
 
 
9.  //*[count(*)=2]
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                        <firstname>Joe</firstname>
                                                        <lastname>User</lastname>
                                                        <fullname>Joe User</fullname>
                                                        <email>Joe.User@gmail.com</email>
                                                        <parentid>John</parentid>
                                                        <groups attName="test_value">groups_content
                                                                  <group>tpadmin</group>
                                                                  <group>operator</group>
                                                        </groups>
                                                        <permissions>
                                                                  <perm type="admin" id="1">POReqSend</perm>
                                                                  <perm type="cert" id="cert_1">CA_CERTS</perm>
                                                                  <perm type="cert" id="cert_2">SYSTEM_CERTS</perm>
                                                                  <perm type="template">POConfirmSend.xfm</perm>
                                                                  <perm type="admin" id="2">POQueryApprover</perm>
                                                                  <perm type="businessprocess">PO.bp</perm>
                                                                  <perm>permission_1</perm>
                                                                  <perm>permission_2</perm>
                                                        </permissions>
                                               </usertoken>
                                      </getUserToken>
                            </UserService>
                   </user_info>
                   <POdocument>
                            <info>
                                      <POnum>RET8999</POnum>
                                      <POdate>2003:11:12</POdate>
                            </info>
                                   ..........
 
Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string. We will see string functions used in perdicates on some examples.
 
10.                   count(//perm)
 
Result: 8
 
11.                   //*[name()='group']
 
Select all elements with name group, equivalent with //group
 
Result:
 
..........
<groups attName="test_value">groups_content
         <group>tpadmin</group>
<group>operator</group>
</groups>
..........
 
12.                  //*[starts-with(name(),'user')]
 
Select all elements name of which starts with the word 'user'
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                        <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                               <username>admin</username>
                                                        <firstname>Joe</firstname>
                                                        <lastname>User</lastname>
                                                        <fullname>Joe User</fullname>
                                                        <email>Joe.User@gmail.com</email>
                                                        <parentid>John</parentid>
                                                        <groups attName="test_value">groups_content
                                                                  <group>tpadmin</group>
                                                                  <group>operator</group>
                                                        </groups>
                                                        <permissions>
                                                                  <perm type="admin" id="1">POReqSend</perm>
                                                                  <perm type="cert" id="cert_1">CA_CERTS</perm>
                                                                  <perm type="cert" id="cert_2">SYSTEM_CERTS</perm>
                                                                  <perm type="template">POConfirmSend.xfm</perm>
                                                                  <perm type="admin" id="2">POQueryApprover</perm>
                                                                  <perm type="businessprocess">PO.bp</perm>
                                                                  <perm>permission_1</perm>
                                                                  <perm>permission_2</perm>
                                                        </permissions>
                                               </usertoken>
                                      </getUserToken>
                            </UserService>
                   </user_info>
                   <POdocument>
                        ..........
 
13.                   //*[contains(name(),'name')]
 
Select all elements name of which contain letter a word 'name'
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                        <firstname>Joe</firstname>
                                                        <lastname>User</lastname>
                                                        <fullname>Joe User</fullname>
                                                        <email>Joe.User@gmail.com</email>
                                                        <parentid>John</parentid>
                                                        <groups attName="test_value">groups_content
                                                                  <group>tpadmin</group>
                                                                  <group>operator</group>
                                                        </groups>
                                                        <permissions>
                                                                  <perm type="admin" id="1">POReqSend</perm>
                                                                 <perm type="cert" id="cert_1">CA_CERTS</perm>
                                                                  <perm type="cert" id="cert_2">SYSTEM_CERTS</perm>
                                                                  <perm type="template">POConfirmSend.xfm</perm>
                                                                  <perm type="admin" id="2">POQueryApprover</perm>
                                                                  <perm type="businessprocess">PO.bp</perm>
                                                                  <perm>permission_1</perm>
                                                                  <perm>permission_2</perm>
                                                        </permissions>
                                               </usertoken>
                                      </getUserToken>
                            </UserService>
                   </user_info>
                   <POdocument>
                            <info>
                                      <POnum>RET8999</POnum>
                                      <POdate>2003:11:12</POdate>
                            </info>
                            <shipTo>
                                      <name>Internet Retailer Inc.</name>
                                      <street>123 Via Way</street>
                                      <city>Milwaukee</city>
                                      <state>WI</state>
                                      <zip>53202</zip>
                            </shipTo>
                            <billTo>
                                      <name>Company name</name>
                                      <street>47 Eden Street</street>
                                      <city>Denver</city>
                                      <state>CO</state>
                                      <zip>80219</zip>
                            </billTo>
                            <items>
                            ..........
admin's picture

XPath - Path Examples

The basic XPath syntax is similar to filesystem addressing. If the path starts with the slash /, then it represents an absolute path to the required element
 
 
1.   /Data
 
Select the root element Data
 
Result:
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                            .............
 
2. /Data/username
 
Select element username that is a child of element Data
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                                  ..........
 
3. /Data/instanceData/user_info/username
 
Select element username which is child of the user_info element, which is child of the instanceData element, which is child of the root element Data
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                                  ..........
If the path starts with // then all elements in the document which fulfill following criteria are selected.
 
4. //username
 
Select all the elements username.
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                        <firstname>Joe</firstname>
                                                        <lastname>User</lastname>
                                                        <fullname>Joe User</fullname>
                                                        <email>Joe.User@gmail.com</email>
                                                        <parentid>John</parentid>
                                                        <groups attName="test_value">groups_content
                                                                  <group>tpadmin</group>
                                                                  <group>operator</group>
                                                        </groups>
                                                        ..........
 
The star * selects all elements located by preceeding path
 
5. /Data/instanceData/user_info/UserService/getUserToken/usertoken/*
 
Select all elements enclosed by elements /Data/instanceData/user_info/UserService/getUserToken/usertoken
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                               <firstname>Joe</firstname>
                                               <lastname>User</lastname>
                                               <fullname>Joe User</fullname>
                                               <email>Joe.User@gmail.com</email>
                                               <parentid>John</parentid>
                                                        <groups attName="test_value">groups_content
                                                                  <group>tpadmin</group>
                                                                  <group>operator</group>
                                                        </groups>
                                               <permissions>
                                                                  <perm type="admin" id="1">POReqSend</perm>
                                                                  <perm type="cert" id="cert_1">CA_CERTS</perm>
                                                                  <perm type="cert" id="cert_2">SYSTEM_CERTS</perm>
                                                                  <perm type="template">POConfirmSend.xfm</perm>
                                                                  <perm type="admin" id="2">POQueryApprover</perm>
                                                                  <perm type="businessprocess">PO.bp</perm>
<perm>permission_1</perm>
                                                                  <perm>permission_2</perm>
                                                        </permissions>
                                               </usertoken>
                                      </getUserToken>
                            </UserService>
                   </user_info>
                   ..........
 
6. /*/*/*/username
 
Select all elements username which have 3 ancestors
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                        <firstname>Joe</firstname>
                                                         ..........
 
 
7. //*
 
Select all elements
 
Result:
 
<?xml version="1.0" encoding="UTF-8"?>
<Data>
        <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
        <username>Joe</username>
        <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                        <firstname>Joe</firstname>
                                                        <lastname>User</lastname>
                                                        <fullname>Joe User</fullname>
                                                        <email>Joe.User@gmail.com</email>
                                                        <parentid>John</parentid>
                                                             <groupsattName="test_value">groups_content
                                                                  <group>tpadmin</group>
                                                                  <group>operator</group>
                                                        </groups>
                                                        <permissions>
                                                                  <perm type="admin" id="1">POReqSend</perm>
                                                       <perm type="cert" id="cert_1">CA_CERTS</perm>
                                                       <perm type="cert" id="cert_1">SYSTEM_CERTS</perm>
                                                       <perm type="template">POConfirmSend.xfm</perm>
                                                       <perm type="admin" id="2">POQueryApprover</perm>
                                                       <perm type="businessprocess">PO.bp</perm>
                                                                  <perm>permission_1</perm>
                                                       <perm>permission_2</perm>
                                                        </permissions>
                                               </usertoken>
                                      </getUserToken>
                            </UserService>
                   </user_info>
                   <POdocument>
                            <info>
                                      <POnum>RET8999</POnum>
                                      <POdate>2003:11:12</POdate>
                            </info>
                            <shipTo>
                                      <name>Internet Retailer Inc.</name>
                                      <street>123 Via Way</street>
                                      <city>Milwaukee</city>
                                      <state>WI</state>
                                      <zip>53202</zip>
                            </shipTo>
                            <billTo>
                                      <name>Company name</name>
                                      <street>47 Eden Street</street>
                                      <city>Denver</city>
                                      <state>CO</state>
                                      <zip>80219</zip>
                            </billTo>
                            <items>
                                      <item>
                                               <productIDdescription="Product_1">CO11</productID>
                                               <quantity>5</quantity>
                                               <price>1.23</price>
                                               <currency>USD</currency>
                                      </item>
                                      <item>
                                               <productIDdescription="Product_2">CO12</productID>
                                               <quantity>7</quantity>
                                               <price>2.34</price>
                                               <currency>USD</currency>
                                      </item>
                                      <item>
                                               <productIDdescription="Product_3">CO13</productID>
                                               <quantity>9</quantity>
                                               <price>3.45</price>
                                               <currency>USD</currency>
                                      </item>
                            </items>
                            <total>53.58</total>
                   </POdocument>
         </instanceData>
</Data>
 
 
admin's picture

XPath - Input Data for Examples

<?xml version="1.0" encoding="UTF-8"?>
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
         <instanceData>
                   <user_info>
                            <username>Joe</username>
                            <UserService>
                                      <getUserToken>
                                               <usertoken>
                                                        <username>admin</username>
                                                        <firstname>Joe</firstname>
                                                        <lastname>User</lastname>
                                                        <fullname>Joe User</fullname>
                                                        <email>Joe.User@gmail.com</email>
                                                        <parentid>John</parentid>
                                                        <groups attName="test_value">groups_content
                                                                  <group>tpadmin</group>
                                                                  <group>operator</group>
                                                        </groups>
                                                        <permissions>
                                                                  <perm type="admin" id="1">POReqSend</perm>
                                                                  <perm type="cert" id="cert_1">CA_CERTS</perm>
                                                                  <perm type="cert" id="cert_2">SYSTEM_CERTS</perm>
                                                                  <perm type="template">POConfirmSend.xfm</perm>
                                                                  <perm type="admin" id="2">POQueryApprover</perm>
                                                                  <perm type="businessprocess">PO.bp</perm>
                                                                  <perm>permission_1</perm>
                                                                  <perm>permission_2</perm>
                                                        </permissions>
                                               </usertoken>
                                      </getUserToken>
                            </UserService>
                   </user_info>
                   <POdocument>
                            <info>
                                      <POnum>RET8999</POnum>
                                      <POdate>2003:11:12</POdate>
                            </info>
                            <shipTo>
                                      <name>Internet Retailer Inc.</name>
                                      <street>123 Via Way</street>
                                      <city>Milwaukee</city>
                                      <state>WI</state>
                                      <zip>53202</zip>
                            </shipTo>
                            <billTo>
                                      <name>Company name</name>
                                      <street>47 Eden Street</street>
                                      <city>Denver</city>
                                      <state>CO</state>
                                      <zip>80219</zip>
                            </billTo>
                            <items>
                                      <item>
                                               <productID description="Product_1">CO11</productID>
                                               <quantity>5</quantity>
                                               <price>1.23</price>
                                               <currency>USD</currency>
                                      </item>
                                      <item>
                                               <productID description="Product_2">CO12</productID>
                                               <quantity>7</quantity>
                                               <price>2.34</price>
                                               <currency>USD</currency>
                                      </item>
                                      <item>
                                               <productID description="Product_3">CO13</productID>
                                               <quantity>9</quantity>
                                               <price>3.45</price>
                                               <currency>USD</currency>
                                      </item>
                            </items>
                            <total>53.58</total>
                   </POdocument>
         </instanceData>
</Data>
An XPath expression returns either a node-set, a string, a Boolean, or a number.
The result of the Xpath is shown in a part of XML bolded. 
Syndicate content