Specifying Relational Operators in XPath Queries

XML and Internet Support

XML and Internet Support

Specifying Relational Operators in XPath Queries

The following examples show how relational operators 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. Specify relational operator

This XPath query returns the <Customer> elements with at least one child <Order> containing an <OrderDetail> child with a Quantity attribute with a value greater than 5:

/child::Customer[Order/OrderDetail[@Quantity>5]]

The predicate specified in the brackets filters the <Customer> elements. Only the <Customer> elements that have at least one <OrderDetail> grandchild with a Quantity attribute value greater than 5 are returned.

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

/Customer[Order/OrderDetail[@Quantity>5]]

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/OrderDetail[@Quantity>5]]
      </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/OrderDetail[@Quantity>5]]?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>
      </Customer>
       ...
    </ROOT>
B. Specify relational operator in the XPath query and use Boolean function to compare the result

This query returns all the <Order> element children of the context node that have an EmployeeID attribute value that is less than 4:

/child::Customer/child::Order[(attribute::EmployeeID < 4)=true()]

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

/Customer/Order[(@EmployeeID < 4)=true()]

Note  When this query is specified in a template, the < character must be entity encoded because the < character has special meaning in an XML document. In a template, use &lt to specify the < character.

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[(@EmployeeID&lt;4)=true()]
        </sql:xpath-query>
    </ROOT>
    
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml