在 XPath 查询中指定算数运算符 (SQLXML 4.0)Specifying Arithmetic Operators in XPath Queries (SQLXML 4.0)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database

以下示例说明如何在 XPath 查询中指定算数运算符。The following example shows how arithmetic operators are specified in XPath queries. 本示例中的 XPath 查询针对 SampleSchema1.xml 中包含的映射架构指定。The XPath query in this example is specified against the mapping schema contained in SampleSchema1.xml. 有关此示例架构的信息,请参阅 (SQLXML 4.0)的 XPath 批注的 XSD 架构示例 For information about this sample schema, see Sample Annotated XSD Schema for XPath Examples (SQLXML 4.0).

示例Examples

A.A. 指定 * 算数运算符Specify the * arithmetic operator

此 XPath 查询返回 <OrderDetail> 满足指定谓词的元素:This XPath query returns <OrderDetail> elements that satisfy the predicate specified:

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

在查询中, child 为轴, OrderDetail 如果 OrderDetail 为,则为节点测试 (TRUE <element node> ,因为 <element> 节点是 轴) 的主节点。In the query, child is the axis and OrderDetail is the node test (TRUE if OrderDetail is an <element node>, because the <element> node is the primary node for the child axis). 对于所有 <OrderDetail> 元素节点,将应用谓词中的测试,并只返回满足条件的那些节点。For all the <OrderDetail> element nodes, the test in the predicate is applied, and only those nodes that satisfy the condition are returned.

备注

XPath 中的数字是双精度浮点数,对本示例中的浮点数进行比较将导致舍入。The numbers in XPath are double-precision floating-point numbers, and comparing floating-point numbers as in the example causes rounding.

针对映射架构测试 XPath 查询To test the XPath query against the mapping schema
  1. 复制 示例架构代码 ,并将其粘贴到文本文件中。Copy the sample schema code and paste it into a text file. 将该文件另存为 SampleSchema1.xml。Save the file as SampleSchema1.xml.

  2. 创建以下模板 (ArithmeticOperatorA.xml),并将它保存在保存 SampleSchema1.xml 的目录中。Create the following template (ArithmeticOperatorA.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">  
        /OrderDetail[@UnitPrice * @OrderQty = 12.350]  
      </sql:xpath-query>  
    </ROOT>  
    

    为映射架构 (SampleSchema1.xml) 指定的目录路径是相对于模板保存目录的相对路径。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. 创建并使用 SQLXML 4.0 测试脚本 (Sqlxml4test.vbs) 执行该模板。Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template.

    有关详细信息,请参阅 使用 ADO 执行 SQLXML 4.0 查询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">  
  <OrderDetail ProductID="Prod-709" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
  <OrderDetail ProductID="Prod-709" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
  <OrderDetail ProductID="Prod-709" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
  <OrderDetail ProductID="Prod-709" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
  <OrderDetail ProductID="Prod-709" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
  <OrderDetail ProductID="Prod-709" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
  <OrderDetail ProductID="Prod-709" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
  <OrderDetail ProductID="Prod-710" UnitPrice="6.175" OrderQty="2" UnitPriceDiscount="0" />   
   ...  
</ROOT>