Specifying Boolean Functions in XPath Queries

XML and Internet Support

XML and Internet Support

Specifying Boolean Functions in XPath Queries

The following examples show how Boolean functions 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 the not() Boolean function

This query returns all the <Customer> child elements of the context node that do not have <Order> subelements:

/child::Customer[not(child::Order)]

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

/Customer[not(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 the virtual name template:
    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="SampleSchema1.xml">
        Customer[not(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[not(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 result:

     <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
        <Customer CustomerID="FISSA" 
                  CompanyName="FISSA Fabrica Inter. Salchichas S.A." 
                      ContactName="Diego Roel" 
                  PostalCode="28034" Country="Spain" 
                      Phone="(91) 555 94 44" Fax="(91) 555 55 93" /> 
        <Customer CustomerID="PARIS" 
                  CompanyName="Paris spécialités" 
                      ContactName="Marie Bertrand" 
                  PostalCode="75012" Country="France" 
                       Phone="(1) 42.34.22.66" Fax="(1) 42.34.22.77" /> 
      </ROOT>
    
B. Specify the true() and false() Boolean functions

This query returns all <Customer> element children of the context node that do not have <Order> subelements. In relational terms, this query returns all customers who have not placed any orders.

/child::Customer[child::Order=false()]

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

/Customer[Order=false()]

This query is equivalent to:

/Customer[not(Order)]

The following query returns all the customers who have placed at least one order:

/Customer[Order=true()]

This query is equivalent to:

/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=false()]
      </sql:xpath-query>
    </ROOT>
    
  2. This URL executes the template:
    http://IISServer/VirtualRoot/template/MyTemplate.xml