Testing the Context Position by Using a Numeric Shortcut

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Testing the Context Position by Using a Numeric Shortcut

In most cases, an XPath predicate has a Boolean value true or false, resulting from the test contained within the [ and ] characters.

A common test appearing in predicates compares the context position to some value. For instance:

chapter[position()=1]

selects the first <chapter> element which is a child of the context node.

XPath provides a convenient shortcut for this common sort of test: simply use the number of the position you want to test. For example, the following behaves exactly like the above:

chapter[1]

The value of the predicate need not be a literal number. It can also be a reference to a variable which has a numeric value, for example, or the value of the number() function applied to some node or text string. Therefore, both of the forms below also demonstrate acceptable uses of this shortcut:

chapter[$xref]
chapter[number("12")]

Note that this shortcut is available only for testing whether the context position is equal to some constant value. There is no similar shortcut for testing greater-than, not-equal-to, and similar conditions.

See Also

Determining the Context Position