在 XPath 查询中指定布尔运算符 (SQLXML 4.0)Specifying Boolean 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 Boolean 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. 指定 OR 布尔运算符Specify the OR Boolean operator

此 XPath 查询将返回 <Customer> CustomerID 属性值为13或31的上下文节点的子元素:This XPath query returns the <Customer> element children of the context node with the CustomerID attribute value of 13 or 31:

/child::Customer[attribute::CustomerID="13" or attribute::CustomerID="31"]  

可以指定 属性 轴 ( @ ) 的快捷方式,因为 轴是默认值,因此可以省略:A shortcut to the attribute axis (@) can be specified, and because the child axis is the default, it can be omitted:

/Customer[@CustomerID="13" or @CustomerID="31"]  

在谓词中, attribute 是轴, CustomerID 是节点测试 (如果 CustomerID 是节点,则为 TRUE <attribute> ,因为 <attribute> 节点是 属性 轴) 的主节点。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). 谓词筛选 <Customer> 元素并仅返回满足谓词中指定的条件的元素。The predicate filters the <Customer> elements and returns only those that satisfy the condition specified in the predicate.

针对映射架构测试 XPath 查询To test the XPath queries 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. 创建以下模板 (BooleanOperatorsA.xml) 并将其保存到保存 SampleSchema1.xml 的目录中。Create the following template (BooleanOperatorsA.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[@CustomerID="13" or @CustomerID="31"]  
      </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 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="31" SalesPersonID="286" TerritoryID="7" AccountNumber="31" CustomerType="S" Orders="Ord-51803 Ord-69427">  
    <Order SalesOrderID="Ord-51803" SalesPersonID="286" OrderDate="2003-08-01T00:00:00" DueDate="2003-08-13T00:00:00" ShipDate="2003-08-08T00:00:00">  
      <OrderDetail ProductID="Prod-718" UnitPrice="1059.31" OrderQty="1" UnitPriceDiscount="0" />   
      <OrderDetail ProductID="Prod-838" UnitPrice="1059.31" OrderQty="1" UnitPriceDiscount="0" />   
    </Order>  
    <Order SalesOrderID="Ord-69427" SalesPersonID="286" OrderDate="2004-05-01T00:00:00" DueDate="2004-05-13T00:00:00" ShipDate="2004-05-08T00:00:00">  
      <OrderDetail ProductID="Prod-835" UnitPrice="440.1742" OrderQty="1" UnitPriceDiscount="0" />   
    </Order>  
  </Customer>  
</ROOT>