22.3. Location PathsNode-sets are returned by location-path expressions. Location paths consist of location steps. Each location step contains an axis and a node test separated by a double colon. That is, a location step looks like this: axis::node test The axis specifies in which direction from the context node the processor searches for nodes. The node test specifies which nodes along that axis are selected. These are some location steps with different axes and node tests: child::set descendant-or-self::node( ) ancestor-or-self::* attribute::xlink:href Each location step may be suffixed with one or more predicates enclosed in square brackets that further winnow the node-set. For example: child::set[position( )=2] descendant-or-self::node( )[.='Eunice'] ancestor-or-self::*[position( )=2][.="Celeste"] attribute::xlink:href[starts-with('http')] Each individual location step is itself a relative location path. The context node against which the relative location path is evaluated is established by some means external to XPath--for example, by the current matched node in an XSLT template. Location steps can be combined by separating them with forward slashes. Each step in the resulting location path sets the context node (or nodes) for the next path in the step. For example: ancestor-or-self::*/child::*[position( )=1] child::document/child::set[position( )=2]/following-sibling::* descendant::node( )[.='Eunice']/attribute::ID An absolute location path is formed by prefixing a forward slash to a relative location path. This sets the context node for the first step in the location path to the root of the document. For example, these are all absolute location paths: /descendant::ship/ancestor-or-self::*/child::*[position( )=1] /child::document/child::set[position( )=2]/following-sibling::* /descendant::node( )[.='Eunice']/attribute::ID Multiple location paths can be combined with the union operator (|) to form an expression that selects a node-set containing all the nodes identified by any of the location paths. For example, this expression selects a node-set containing all the set children of the context node, all the vector descendants of the context node, all ancestor elements of the context node and the root node, and all attributes of the context node named href: child::set | descendant::vector | ancestor::* | attribute::href 22.3.1. Abbreviated SyntaxAn abbreviated syntax is available for particularly common location steps. This is the syntax allowed in XSLT match patterns. In this syntax, five axes may use this shorthand:
Using the abbreviated syntax, the previous examples can be rewritten in the following manner: set descendant-or-self::node( ) ancestor-or-self::* @xlink:href set[position( )=2] descendant-or-self::node( )[.='Eunice'] ancestor-or-self::*[position( )=2][.="Celeste"] @xlink:href[starts-with('http')] ancestor-or-self::*/*[position( )=1] document/set[position( )=2]/following-sibling::* descendant::node( )[.='Eunice']/@ID //ship/ancestor-or-self::*/*[position( )=1] /document/set[position( )=2]/following-sibling::* /descendant::node( )[.='Eunice']/@ID set | .//vector | ancestor::* | @href Not all location steps can be rewritten using the abbreviated syntax. In particular, only the child, self, attribute, descendant-or-self, and parent axes can be abbreviated. The remaining axes must be spelled out in full. 22.3.2. AxesEach XPath location step moves along an axis from a context node. The context node is a particular root, element, attribute, comment, processing-instruction, namespace, or text node in the XML document. (In practice, it's almost always an element node or the root node.) The context node may be a node selected by the previous location step in the location path, or it may be determined by extra-XPath rules, such as which node is matched by an xsl:template element. However the context node is determined, it has some relationship to every other node in the document. The various axes divide the document into different, overlapping sets, depending on their relationship to the context node. There are exactly 13 axes you can use in an XPath location step:
22.3.3. Node TestsEach location step has at least an axis and a node test. The node test further refines the nodes selected by the location step. In an unabbreviated location step, a double colon (::) separates the axis from the node test. There are seven kinds of node tests:
Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|