Specifying Selection Predicates in the Location Path

XML and Internet Support

XML and Internet Support

Specifying Selection Predicates in the Location Path

A predicate filters a node-set with respect to an axis (similar to a WHERE clause in a SELECT statement). The predicate is specified between brackets. For each node in the node-set to be filtered, the predicate expression is evaluated with that node as the context node, with the number of nodes in the node-set as context size. If the predicate expression evaluates to TRUE for that node, the node is included in the resulting node-set.

XPath also allows position-based filtering. A predicate expression evaluating to a number selects that ordinal node. For example, the location path Customer[3] returns the third customer. Such numeric predicates are not supported. Only predicate expressions that return a Boolean result are supported.

Note  For information about the limitations of this XPath implementation of XPath and the differences between it and the W3C specification, see XPath Guidelines and Limitations.

Selection Predicate: Example 1

This XPath expression (location path) selects from the current context node all the <Customer> element children that have the CustomerID attribute with value of ALFKI:

/child::Customer[attribute::CustomerID="ALFKI"]

In this XPath query, child, and attribute are the axis name. Customer is the node test (TRUE if Customer is an <element node>, because <element> is the principal node type for the child axis). attribute::CustomerID="ALFKI" is the predicate. In the predicate, attribute is the axis and CustomerID is the node test (TRUE if CustomerID is an attribute of the context node, because <attribute> is the principal node type of attribute axis).

Using the abbreviated syntax, the XPath query can also be specified as:

/Customer[@CustomerID="ALFKI"]
Selection Predicate: Example 2

This XPath expression (location path) selects from the current context node all the <Order> grandchildren that have the OrderID attribute with the value 1:

/child::Customer/child::Order[attribute::OrderID="1"]

In this XPath expression, child and attribute are the axis names. Customer, Order, and OrderID are the node tests. attribute::OrderID="1" is the predicate.

Using the abbreviated syntax, the XPath query can also be specified as:

/Customer/Order[@OrderID="1"]
Selection Predicate: Example 3

This XPath expression (location path) selects from the current context node all the <Customer> children that have one or more <ContactName> children:

child::Customer[child::ContactName]

The example assumes that the <ContactName> is a <child> element of the <Customer> element in the XML document, which is referred to as element-centric mapping in an annotated XDR schema. For more information, see Creating XML Views Using Annotated XDR Schemas.

In this XPath expression, child is the axis name. Customer is the node test (TRUE if Customer is an <element> node, because <element> is the principal node type for child axis). child::ContactName is the predicate. In the predicate, child is the axis and ContactName is the node test (TRUE if ContactName is an <element> node).

This expression returns only the <Customer> element children of the context node that have <ContactName> element children.

Using the abbreviated syntax, the XPath query can also be specified as:

Customer[ContacName]
Selection Predicate: Example 4

This XPath expression selects <Customer> element children of the context node that do not have <ContactName> element children:

child::Customer[not(child::ContactName)]

The example assumes that the <ContactName> is a subelement of <Customer> element in the XML document and the ContactName is a field that is not required in the database.

In this example, child is the axis. Customer is the node test (TRUE if Customer is an <element> node). not(child::ContactName) is the predicate. In the predicate child is the axis and ContactName is the node test (TRUE if ContactName is an <element> node).

Using the abbreviate syntax, the XPath query can also be specified as:

Customer[not(ContactName)]
Selection Predicate: Example 5

This XPath expression selects from the current context node all the <Customer> children that have the CustomerID attribute:

child::Customer[attribute::CustomerID]

In this example, child is the axis and Customer is node test (TRUE if Customer is an <element> node). attribute::CustomerID is the predicate. In the predicate, attribute is the axis and CustomerID is the predicate (TRUE if CustomerID is an <attribute> node).

Using the abbreviated syntax, the XPath query can also be specified as:

Customer[@CustomerID]

See Also

Creating XML Views Using Annotated XDR Schemas

Retrieving XML Documents Using FOR XML

Accessing SQL Server Using HTTP