Specifying Axes in XPath Queries

XML and Internet Support

XML and Internet Support

Specifying Axes in XPath Queries

The following examples show how axes are specified in XPath queries. The XPath queries in these examples are specified against the mapping schema contained in SampleSchema1.xml. For information about this sample schema, see Sample XPath Queries.

Examples
A. Retrieve child elements of the context node

This XPath query selects all the <Customer> child elements of the context node:

/child::Employee

In the query, child is the axis and Customer is the node test (TRUE if Customer is an <element> node, because <element> is the primary node type associated with the child axis).

The child axis is the default. Therefore, the query can be written as:

/Employee

To test the XPath query against the mapping schema

  1. Create the following template (MyTemplate.xml) and save it in the directory associated with template virtual name:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema2.xml">
        /Employee
      </sql:xpath-query>
    </ROOT>
    
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml
    

    This XPath query can be specified directly in the URL:

    http://IISServer/nwind/schema/SampleSchema1.xml/child::Customer?root=root

    The virtual name schema is of schema type. The schema file is stored in the directory associated with virtual name of schema type. The root parameter is used to specify a top-level element for the resulting XML document (root can be any value).

    Here is the partial result set of the template execution:

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> 
      <Employee EmployeeID="1" LastName="Davolio" 
                FirstName="Nancy" Title="Sales Representative" /> 
      <Employee EmployeeID="2" LastName="Fuller" 
                FirstName="Andrew" Title="Vice President, Sales" /> 
       ...
    </ROOT>
    
B. Retrieve grandchildren of the context node

This XPath query selects all the <Order> element children of the <Customer> element children of the context node:

/child::Customer/child::Order

In the query, child is the axis and Customer and Order are the node tests (these node tests are TRUE if Customer and Order are <element> nodes, because the <element> node is the primary node for the child axis). For each node matching <Customer>, the nodes matching <Orders> are added to the result. Only <Order> is returned in the result set.

The child axis is the default. Therefore, the query can be specified as:

/Customer/Order

To test the XPath query against the mapping schema

  1. Create the following template (MyTemplate.xml) and save it in the directory associated with template virtual name:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        /Customer/Order
      </sql:xpath-query>
    </ROOT>
    
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml
    
    This XPath query can be specified directly in the URL:
    http://IISServer/nwind/schema/SampleSchema1.xml/Customer/Order?root=root

    The virtual name schema is of schema type. The schema file is stored in the directory associated with virtual name of schema type. The root parameter is used to specify a top-level element for the resulting XML document (root can be any value).

    Here is the partial result set of the template execution:

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <Order OrderID="Ord-10643" EmployeeID="6" 
             OrderDate="1997-08-25T00:00:00" 
             RequiredDate="1997-09-22T00:00:00" 
             ShippedDate="1997-09-02T00:00:00">
        <OrderDetail ProductID="Prod-28" UnitPrice="45.6" Quantity="15">
          <Discount>0.25</Discount> 
        </OrderDetail>
        <OrderDetail ProductID="Prod-39" UnitPrice="18" Quantity="21">
          <Discount>0.25</Discount> 
        </OrderDetail>
        <OrderDetail ProductID="Prod-46" UnitPrice="12" Quantity="2">
          <Discount>0.25</Discount> 
        </OrderDetail>
      </Order>
       ...
    </ROOT>
    

If the XPath query is specified as Customer/Order/OrderDetail, from each node matching <Customer>, the query navigates to their <Order> elements. And for each node matching <Order>, the query adds the nodes <OrderDetail> to the result. Only <OrderDetail> is returned in the result set.

C. Use .. to specify the parent axis

This query retrieves all the <Order> elements whose parent is <Customer> element with a CustomerID attribute value of ALFKI. The query uses parent axis in the predicate to find parent of <Order> element.

/child::Customer/child::Order[../@CustomerID="ALFKI"]

The child axis is the default axis. Therefore, the query can be specified as:

/Customer/Order[../@CustomerID="ALFKI"]

The XPath query is equivalent to:

/Customer[@CustomerID="ALFKI"]/Order.

Note  The XPath query /Order[../@CustomerID="ALFKI"] will return an error because there is no parent of Order. Although there may be elements in the mapping schema that contain Order, the XPath did not begin at any of them; consequently, Order is considered to be the top-level element type in the document.

To test the XPath query against the mapping schema

  1. Create the following template (MyTemplate.xml) and save it in the directory associated with template virtual name:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        /Customer/Order[../@CustomerID="ALFKI"]
      </sql:xpath-query>
    </ROOT>
    
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml
    
  3. Here is the partial result set of the template execution:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> 
    <Order OrderID="Ord-10643" EmployeeID="6" 
              OrderDate="1997-08-25T00:00:00" 
              RequiredDate="1997-09-22T00:00:00" 
              ShippedDate="1997-09-02T00:00:00"> 
      <OrderDetail ProductID="Prod-28" UnitPrice="45.6" Quantity="15"> 
        <Discount>0.25</Discount> 
      </OrderDetail> 
      <OrderDetail ProductID="Prod-39" UnitPrice="18" Quantity="21"> 
        <Discount>0.25</Discount> 
      </OrderDetail> 
      <OrderDetail ProductID="Prod-46" UnitPrice="12" Quantity="2"> 
        <Discount>0.25</Discount> 
      </OrderDetail> 
       ...
    </Order>
    </ROOT>
    
D. Specify the attribute axis

This XPath query selects all the <Customer> child elements of the context node with a CustomerID attribute value of ALFKI:

/child::Customer[attribute::CustomerID="ALFKI"]

In the predicate attribute::CustomerID, attribute is the axis and CustomerID is the node test (if CustomerID is an attribute the node test is TRUE, because the <attribute> node is the primary node for the attribute axis).

A shortcut to the attribute axis (@) can be specified, and because child is the default axis, it can be omitted from the query:

/Customer[@CustomerID="ALFKI"]

To test the XPath query against the mapping schema

  1. Create the following template (MyTemplate.xml) and save it in the directory associated with template virtual name:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        child::Customer[attribute::CustomerID="ALFKI"]
      </sql:xpath-query>
    </ROOT>
    
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml
    

    This XPath query can be specified directly in the URL:

    http://IISServer/nwind/schema/SampleSchema1.xml/Customer[@CustomerID="ALFKI"]?root=root
    

    The virtual name schema is of schema type. The schema file is stored in the directory associated with virtual name of schema type. The root parameter is used to specify a top-level element for the resulting XML document (root can be any value).

    Here is the partial result set of the template execution:

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <Customer CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" 
                     ContactName="Maria Anders" PostalCode="12209" 
                     Country="Germany" Phone="030-0074321" Fax="030-0076545" 
                     Orders="Ord-10643 Ord-10692 Ord-10702 Ord-10835 Ord-10952 
                             Ord-11011">
        <Order OrderID="Ord-10643" EmployeeID="6" 
                   OrderDate="1997-08-25T00:00:00" 
                   RequiredDate="1997-09-22T00:00:00" 
                   ShippedDate="1997-09-02T00:00:00">
             <OrderDetail ProductID="Prod-28" UnitPrice="45.6" Quantity="15">
                  <Discount>0.25</Discount> 
              </OrderDetail>
             <OrderDetail ProductID="Prod-39" UnitPrice="18" Quantity="21">
                  <Discount>0.25</Discount> 
              </OrderDetail>
             <OrderDetail ProductID="Prod-46" UnitPrice="12" Quantity="2">
                  <Discount>0.25</Discount> 
              </OrderDetail>
      </Order>
       ...
    </ROOT>