Specifying Boolean Functions in XPath Queries (SQLXML 4.0)

Applies to: SQL Server Azure SQL Database

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

Examples

A. Specify the not() Boolean function

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

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

  2. Create the following template (BooleanFunctionsA.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[not(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 set of the template execution:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">  
  <Customer CustomerID="13" SalesPersonID="286" TerritoryID="7" AccountNumber="13" CustomerType="S" />   
  <Customer CustomerID="32" SalesPersonID="289" TerritoryID="8" AccountNumber="32" CustomerType="S" />   
  <Customer CustomerID="35" SalesPersonID="275" TerritoryID="2" AccountNumber="35" CustomerType="S" />   
  ...  
</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> child elements. 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 the following:

/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 this one:

/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 (BooleanFunctionsB.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=false()]  
      </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 set of the template execution:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">  
  <Customer CustomerID="13" SalesPersonID="286" TerritoryID="7" AccountNumber="13" CustomerType="S" />   
  <Customer CustomerID="32" SalesPersonID="289" TerritoryID="8" AccountNumber="32" CustomerType="S" />   
  <Customer CustomerID="35" SalesPersonID="275" TerritoryID="2" AccountNumber="35" CustomerType="S" />   
  ...  
</ROOT>