Specifying Arithmetic Operators in XPath Queries

XML and Internet Support

XML and Internet Support

Specifying Arithmetic Operators in XPath Queries

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

Examples
A. Specify the * arithmetic operator

This XPath query returns <OrderDetail> elements that satisfy the predicate specified:

/child::OrderDetail[@UnitPrice * @Quantity = 98]

In the query, child is the axis and OrderDetail is the node test (TRUE if OrderDetail is an <element node>, because <element> node is the primary node for the child axis). For all the <OrderDetail> element nodes, the test in the predicate is applied, and only those nodes that satisfy the condition are returned.

Note  The numbers in XPath are double-precision floating-point numbers, and comparing floating-point numbers as in the example causes rounding.

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">
        /OrderDetail[@UnitPrice * @Quantity = 98]
      </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/OrderDetail[@UnitPrice*@Quantity=98]?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). An arithmetic operator cannot be used when an XPath query is specified directly in the URL, because the plus sign (+) has a special meaning in the URL. This issue does not arise if the query is specified in the template.

    Here is the partial result set of the template execution:

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <OrderDetail ProductID="Prod-33" UnitPrice="2" Quantity="49">
        <Discount>0</Discount> 
      </OrderDetail>
      <OrderDetail ProductID="Prod-42" UnitPrice="9.8" Quantity="10">
        <Discount>0</Discount> 
      </OrderDetail>
      <OrderDetail ProductID="Prod-52" UnitPrice="7" Quantity="14">
        <Discount>5.0000001E-2</Discount> 
      </OrderDetail>
      <OrderDetail ProductID="Prod-67" UnitPrice="14" Quantity="7">
        <Discount>0</Discount> 
      </OrderDetail>
       ...
    </ROOT>