display | more...

XPath - Next: XPath Axis

node
A node refers to some object, usually an element or an attribute, or possibly a namespace, a processing instruction, a comment, or text.
context node
The context node refers to the node which is current being processed. If an XSLT processor is working on a stylesheet, the context node is usually the node matched by an xsl:template or xsl:for-each directive.
principal node type
The principal node type is the type of the context node. The type will be one of element, attribute, namespace, processing instruction, comment, or text.
location step
An XPath expression is built from multiple location steps connected together with a slash(/). A location step locates a particular node in a particular axis, and an optional predicate.
axis
An axis refers to a set of nodes with some well defined relationship to the context node.
node test
A node test is usually just the name of a node, or some pattern that matches the names of nodes; it is used to find matching nodes in the source document. Node tests may use * to match all nodes of the principal node type in a particular axis. A node test of node() returns all nodes, while a node test of text() returns all of the text nodes. Similarly, processing-instruction() returns all of the processing instruction nodes, and comment() returns comment nodes. Refer to http://www.w3.org/TR/xpath#node-tests, section 2.3 of the XPath specification for more information.
pattern
A pattern in XPath is basically an XPath expression, but with only a subset of the functionality. A pattern may only return results that are nodesets, its location steps can only use the child or attribute axes. Patterns may not use the descendant-or-self axis, but the // and / operators are still valid for patterns.
predicate
A predicate is used to filter the results of the node test. A predicate is basically another XPath expression evaluated for each node in the node-set returned by the node test, except that you can also use an integer to get a specific node along the axis. Predicates may be used to limit the results of a node test to those nodes that have certain attributes or relationships to other nodes. Expressions in a predicate can be combined by using the logical "and" and "or" operators. Multiple predicates can be applied to the results of a node test by simply appending (not nesting) more square bracket pairs containing predicate expressions. See http://www.w3.org/TR/xpath#predicates, section 2.4 of the XPath specification for more information.

The syntax for building a location step is pretty straight forward. It consists of three parts; the axis name, the node test, and the predicate. The axis name is connected to the node test with double colons (e.g. parent::animal), and the predicate is enclosed in square brackets.

XPath location steps can be connected with a /, just like in a (UNIX) directory structure. If we wanted to start from the root of the document and reference the people nodes, we would use an expression like this: /zoo/people/. Like a directory structure, an empty location step / refers to the root node of the document.

The child axis is the default axis; it is used if the axis is omitted from the XPath expression. Two other syntax notes worthy of mention are that the parent axis may be abbreviated as .. (e.g. use ../animals instead of parent::animals), and the attribute axis may be abbreviated as @ (e.g. use @job instead of attribute::job).

If you want to use a node test to get the current context node, then the node() function provides this.

Log in or register to write something here or to contact authors.