在位置路径中指定节点测试 (SQLXML 4.0)

节点测试指定根据位置步骤选择的节点类型。每个轴(child、parent、attribute 或 self)都具有主要节点类型。对于 attribute 轴,主要节点类型为 <attribute>。对于 parent、child 和 self 轴,主要节点类型为 <element>

注意注意

不支持通配符节点测试 *(例如 child::*)。

节点测试:示例 1

位置路径 child::Customer 选择上下文节点的 <Customer> 元素子级。

在本示例中,child 是轴,Customer 是节点测试。child 轴的主要节点类型为 <element>。因此,如果 <Customer> 节点是一个 <element> 节点,节点测试为 TRUE。如果上下文节点没有任何 <Customer> 子级,则返回空节点集。

节点测试:示例 2

位置路径 attribute::CustomerID 选择上下文节点的 CustomerID 属性。

在本示例中,attribute 是轴,CustomerID 是节点测试。attribute 轴的主要节点类型为 <attribute>。因此,如果 CustomerID 是一个 <attribute> 节点,节点测试为 TRUE。如果上下文节点没有任何 CustomerID 子级,则返回空节点集。

注意注意

在这一 XPath 实现中,如果某位置步骤引用架构中未声明的 <element><attribute> 类型,将生成错误。这与 MSXML 中的 XPath 实现不同,在 MSXML 中返回空节点集。

轴的缩写语法

支持以下位置路径的缩写语法:

  • attribute:: 可缩写为 @。

    位置路径 Customer[@CustomerID="ALFKI"] 与 child::Customer[attribute::CustomerID="ALFKI"] 相同。

  • 可以从位置步骤中省略 child::。

    因此,child 为默认轴。位置路径 Customer/Order 与 child::Customer/child::Order 相同。

  • self::node() 可缩写为一个句点 (.),parent::node() 可缩写为两个句点 (..)。