Specifying Boolean Operators in XPath Queries

XML and Internet Support

XML and Internet Support

Specifying Boolean Operators in XPath Queries

The following example shows how Boolean operators are specified in XPath queries. The XPath queries in this examples is specified against the mapping schema contained in SampleSchema1.xml. For information about this sample schema, see Sample XPath Queries.

Examples
A. Specify the OR Boolean operator

This XPath query returns the <Customer> element children of the context node with the CustomerID attribute value of ALFKI or ANATR:

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

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

/Customer[@CustomerID="ALFKI" or @CustomerID="ANATR"]

In the predicate, attribute is the axis and CustomerID is the node test (TRUE if CustomerID is an <attribute> node, because the <attribute> node is the primary node for the attribute axis). The predicate filters the <Customer> elements and returns only those that satisfy the condition specified in the predicate.

To test the XPath queries 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" or @CustomerID="ANATR"]
      </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" or @CustomerID="ANATR"]?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).