XPath - Path Examples

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