XPath - Node Set and Predicates Examples

admin's picture
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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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>
                            ..........