Specifying Boolean-Valued Predicates in XPath Queries

XML and Internet Support

XML and Internet Support

Specifying Boolean-Valued Predicates in XPath Queries

The following examples show how Boolean-valued predicates 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 multiple predicates

This XPath query uses multiple predicates to find order information for a given order ID and a customer ID:

/child::Customer[attribute::CustomerID="ALFKI"]/child::Order[attribute::OrderID="Ord-10643"]

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[@CustomerID="ALFKI"]/Order[@OrderID="Ord-10643"]

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[@CustomerID="ALFKI"]/Order[@OrderID="Ord-10643"]
      </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"]/Order[@OrderID="Ord-10643"]?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:

    <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>
    
B. Specify successive and nested predicates

This query shows using successive predicates. The query returns all the <Customer> child elements of the context node that have both a City attribute with a value of London and a Fax attribute:

/child::Customer[attribute::City="London"][attribute::Fax]

The query returns the <Customer> elements that satisfy both the conditions specified in the predicates.

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[@City="London"][@Fax]

The following XPath query illustrates the use of nested predicates. The query returns all the <Customer> child elements of the context node that include <Order> subelements with at least one of <Order> element that has an EmployeeID attribute value of 2.

/Customer[Order[@EmployeeID=2]]

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[@City="London"][@Fax]
      </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[@City="London"][@Fax]?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).

C. Specify a top-level predicate

This query returns the <Customer> child element nodes of the context node that have <Order> element children. The query tests the location path as the top-level predicate:

/child::Customer[child::Order]

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:

    <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>