Accessing XPath nodes

All the XPath operators and XPath axes can be used to access the nodes.

For the complete list of operators and axes on XPath, see the XML Path Language (XPath) 3.0 standards.

The following code block is an XML document sample that is used in the examples:


<PurchaseOrderRequest>
  <PurchaseOrder>
    <deliverTo>
      <PhysicalAddress>
        <cityName>
          <FreeFormText>Reston VA</FreeFormText>
        </cityName>
        <addressLine1>
          <FreeFormText>IBM</FreeFormText>
        </addressLine1>
        <addressLine2>
          <FreeFormText>11700 Plaza America Drive</FreeFormText>
        </addressLine2>
        <addressLine3>
          <FreeFormText>Reston VA</FreeFormText>
        </addressLine3>
        <NationalPostalCode>20190</NationalPostalCode>
      </PhysicalAddress>
    </deliverTo>
    <comment>
      <FreeFormText>Comments go here</FreeFormText>
    </comment>
    <packListRequirements>
      <FreeFormText>Packing List Requirements</FreeFormText>
    </packListRequirements>
    <totalCost free="noMonetaryValue">6510</totalCost>
    <!-- This is the items array series -->
    <!-- Item 1 -->
     <ProductLineItem>
      <shipFrom>
        <GlobalLocationIdentifier>Warehouse 1</GlobalLocationIdentifier>
      </shipFrom>
      <ProductQuantity>150</ProductQuantity>
      <LineNumber>1</LineNumber>
      <productUnit>
        <ProductPackageDescription>
          <ProductIdentification>
            <GlobalProductIdentifier>Anvil</GlobalProductIdentifier>
            <PartnerProductIdentification>
              <GlobalPartnerClassificationCode>5644</GlobalPartnerClassificationCode>
              <ProprietaryProductIdentifier>CS-1100</ProprietaryProductIdentifier>
            </PartnerProductIdentification>
          </ProductIdentification>
        </ProductPackageDescription>
      </productUnit>
    </ProductLineItem>
    <!-- Item 2 -->
     <ProductLineItem>
      <shipFrom>
        <GlobalLocationIdentifier>Computer Warehouse 1</GlobalLocationIdentifier>
      </shipFrom>
      <ProductQuantity>120</ProductQuantity>
      <LineNumber>2</LineNumber>
      <productUnit>
        <ProductPackageDescription>
          <ProductIdentification>
            <GlobalProductIdentifier>Hammer</GlobalProductIdentifier>
            <PartnerProductIdentification>
              <GlobalPartnerClassificationCode>5672</GlobalPartnerClassificationCode>
              <ProprietaryProductIdentifier>CS-1150</ProprietaryProductIdentifier>
            </PartnerProductIdentification>
          </ProductIdentification>
        </ProductPackageDescription>
      </productUnit>
    </ProductLineItem>
  </PurchaseOrder>
</PurchaseOrderRequest>

The following table lists some path expressions and the result of the expressions based on the sample XML document:

Example String XPath Results
PurchaseOrder Selects all nodes with the name PurchaseOrder.
/PurchaseOrder Selects the root element PurchaseOrder.
PurchaseOrder/deliverTo Selects the deliverTo element.
<deliverTo>
         <PhysicalAddress>
            <cityName>
               <FreeFormText>Reston VA</FreeFormText>
            </cityName>
            <addressLine1>
               <FreeFormText>IBM</FreeFormText>
            </addressLine1>
            <addressLine2>
               <FreeFormText>11700 Plaza America Drive</FreeFormText>
            </addressLine2>
            <addressLine3>
               <FreeFormText>Reston VA</FreeFormText>
            </addressLine3>
            <NationalPostalCode>20190</NationalPostalCode>
          </PhysicalAddress>
       </deliverTo>
//deliverTo Selects all deliverTo elements in the document.
PurchaseOrder//deliverTo Selects all book elements that are descendant of the *PurchaseOrder* element.
<FreeFormText>Reston VA</FreeFormText>
//@free Selects all attributes that are named free. noMonetaryValue
//ProductLineItem[ProductQuantity>120]/ProductQuantity Selects the productLineItem only if the productQuantity is greater than 120.
<ProductQuantity>150</ProductQuantity>
//PurchaseOrder/totalCost | //ProductLineItem[ProductQuantity>120]/ProductQuantity Selects all the totalCost AND ProductQuantity, if the ProductQuantity is more than 120.
<result>
<totalCost free="noMonetaryValue">6510</totalCost>
<ProductQuantity>150</ProductQuantity>
<ProductQuantity>120</ProductQuantity>
</result>
//PurchaseOrder/ProductLineItem[last()] Selects the last *ProductLineItem* element that is the child of the *PurchaseOrder* element.
<ProductLineItem>
      <shipFrom>
        <GlobalLocationIdentifier>Computer Warehouse 1</GlobalLocationIdentifier>
      </shipFrom>
      <ProductQuantity>120</ProductQuantity>
      <LineNumber>2</LineNumber>
      <productUnit>
        <ProductPackageDescription>
          <ProductIdentification>
            <GlobalProductIdentifier>Hammer</GlobalProductIdentifier>
            <PartnerProductIdentification>
              <GlobalPartnerClassificationCode>5672</GlobalPartnerClassificationCode>
              <ProprietaryProductIdentifier>CS-1150</ProprietaryProductIdentifier>
            </PartnerProductIdentification>
          </ProductIdentification>
        </ProductPackageDescription>
      </productUnit>
</ProductLineItem>
* ​ (Wildcard) Matches any element node. ​
@* ​ (Wildcard) Matches any attribute node. ​
node() ​ (Wildcard) Matches any node of any kind. ​
/PurchaseOrder/* ​ Selects all the child element nodes of the ​ PurchaseOrder element. ​
//* ​ Selects all elements in the document. ​
//totalCost[@*] ​ Selects all title elements, which have at ​ least one attribute of any kind.
<totalCost free="noMonetaryValue">6510</totalCost>