Specifying Boolean-Valued Predicates in XPath Queries (SQLXML 4.0)

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 Annotated XSD Schema for XPath Examples (SQLXML 4.0).

Examples

A. Specify multiple predicates

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

/child::Customer[attribute::CustomerID="1"]/child::Order[attribute::OrderID="Ord-43860"]

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="1"]/Order[@SalesOrderID="Ord-43860"]

To test the XPath query against the mapping schema

  1. Copy the sample schema code and paste it into a text file. Save the file as SampleSchema1.xml.

  2. Create the following template (BooleanValuedPredicatesA.xml) and save it in the directory where SampleSchema1.xml is saved.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        /Customer[@CustomerID="1"]/Order[@SalesOrderID="Ord-43860"]
      </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema (SampleSchema1.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example:

    mapping-schema="C:\MyDir\SampleSchema1.xml"
    
  3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template.

    For more information, see Using ADO to Execute SQLXML 4.0 Queries.

    Here is the result:

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <Order SalesOrderID="Ord-43860" SalesPersonID="280" OrderDate="2001-08-01T00:00:00" DueDate="2001-08-13T00:00:00" ShipDate="2001-08-08T00:00:00">
        <OrderDetail ProductID="Prod-729" UnitPrice="226.8571" OrderQty="1" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-732" UnitPrice="440.1742" OrderQty="1" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-738" UnitPrice="220.2496" OrderQty="1" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-753" UnitPrice="2576.3544" OrderQty="2" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-756" UnitPrice="1049.7528" OrderQty="1" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-758" UnitPrice="1049.7528" OrderQty="2" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-761" UnitPrice="503.3507" OrderQty="2" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-762" UnitPrice="503.3507" OrderQty="1" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-763" UnitPrice="503.3507" OrderQty="1" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-765" UnitPrice="503.3507" OrderQty="2" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-768" UnitPrice="503.3507" OrderQty="1" UnitPriceDiscount="0" /> 
        <OrderDetail ProductID="Prod-770" UnitPrice="503.3507" OrderQty="1" UnitPriceDiscount="0" /> 
      </Order>
    </ROOT>
    

B. Specify successive and nested predicates

The following query shows using successive predicates. The query returns all the <Customer> child elements of the context node that have both a SalesPersonID attribute with a value of 277 and a TerritoryID attribute with a value of 3:

/child::Customer[attribute::SalesPersonID="277"][attribute::TerritoryID="3"]

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[@SalesPersonID="277"][@TerritoryID="3"]

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> child elements with at least one <Order> element that has a SalesPersonID attribute value of 2.

/Customer[Order[@SalesPersonID=2]]

To test the XPath query against the mapping schema

  1. Copy the sample schema code and paste it into a text file. Save the file as SampleSchema1.xml.

  2. Create the following template (nestedSuccessive.xml) and save it in the directory where SampleSchema1.xml is saved.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
             /Customer[@SalesPersonID="277"][@TerritoryID="3"]
      </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema (SampleSchema1.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example:

    mapping-schema="C:\MyDir\SampleSchema1.xml"
    
  3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template.

    For more information, see Using ADO to Execute SQLXML 4.0 Queries.

The following is a partial result:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <Customer CustomerID="22" SalesPersonID="277" TerritoryID="3" 
            AccountNumber="22" CustomerType="S" 
            Orders="Ord-43874 Ord-44519 Ord-46989 Ord-48013 Ord-49130 Ord-50274 Ord-51807 Ord-57113 Ord-63162 Ord-69495">
    <Order SalesOrderID="Ord-43874" SalesPersonID="277" 
           OrderDate="2001-08-01T00:00:00" 
           DueDate="2001-08-13T00:00:00" 
           ShipDate="2001-08-08T00:00:00">
      <OrderDetail ProductID="Prod-763" UnitPrice="503.3507" 
                   OrderQty="1" UnitPriceDiscount="0" /> 
    </Order>
    ...
  </Customer>
  <Customer CustomerID="39" SalesPersonID="277" TerritoryID="3" 
            AccountNumber="39" CustomerType="S" 
            Orders="Ord-47428 Ord-48367 Ord-49529 Ord-50742 Ord-53591 Ord-59051 Ord-65301 Ord-71912">    <Order SalesOrderID="Ord-47428" SalesPersonID="277" 
           OrderDate="2002-09-01T00:00:00" 
           DueDate="2002-09-13T00:00:00" 
           ShipDate="2002-09-08T00:00:00">
      <OrderDetail ProductID="Prod-759" UnitPrice="563.7528" OrderQty="2" UnitPriceDiscount="0" /> 
      <OrderDetail ProductID="Prod-769" UnitPrice="563.7528" OrderQty="1" UnitPriceDiscount="0" /> 
      ...
    </Order>
    ...
  </Customer>
  ...
</ROOT>

C. Specify a top-level predicate

The following 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. Copy the sample schema code and paste it into a text file. Save the file as SampleSchema1.xml.

  2. Create the following template (TopLevelPredicate.xml) and save it in the directory where SampleSchema1.xml is saved.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        /Customer[Order]
      </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema (SampleSchema1.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example:

    mapping-schema="C:\MyDir\SampleSchema1.xml"
    
  3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template.

    For more information, see Using ADO to Execute SQLXML 4.0 Queries.

Here is the partial result:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <Customer CustomerID="1" SalesPersonID="280" TerritoryID="1" AccountNumber="1" CustomerType="S" Orders="Ord-43860 Ord-44501 Ord-45283 Ord-46042">
    <Order SalesOrderID="Ord-43860" SalesPersonID="280" OrderDate="2001-08-01T00:00:00" DueDate="2001-08-13T00:00:00" ShipDate="2001-08-08T00:00:00">
      <OrderDetail ProductID="Prod-729" UnitPrice="226.8571" OrderQty="1" UnitPriceDiscount="0" /> 
      <OrderDetail ProductID="Prod-732" UnitPrice="440.1742" OrderQty="1" UnitPriceDiscount="0" /> 
      <OrderDetail ProductID="Prod-738" UnitPrice="220.2496" OrderQty="1" UnitPriceDiscount="0" /> 
      ...
    </Order>
    <Order SalesOrderID="Ord-44501" SalesPersonID="280" OrderDate="2001-11-01T00:00:00" DueDate="2001-11-13T00:00:00" ShipDate="2001-11-08T00:00:00">
      <OrderDetail ProductID="Prod-725" UnitPrice="226.8571" OrderQty="3" UnitPriceDiscount="0" /> 
      <OrderDetail ProductID="Prod-726" UnitPrice="226.8571" OrderQty="2" UnitPriceDiscount="0" /> 
      ...
    </Order>    ...
  </Customer>
  ...
</ROOT>