Book HomeXSLTSearch this book

Appendix B. XPath Reference

This appendix contains reference information from the XPath specification. The XPath node types, axes, and operators are defined here. The datatypes used in XSLT stylesheets, including the result tree fragment type (technically defined in the XSLT specification, not in XPath), are defined here as well. This appendix concludes with a definition of the XPath context.

B.1. XPath Node Types

There are seven types of nodes in XPath. We'll stick to the reference material here; for more information on the different node types, see our earlier discussion of the XPath data model.

B.1.1. The Root Node

The root node is the root of the tree. Unlike all other nodes, it does not have a parent. Its children are the element node for the document, along with any comments or processing instructions that appear outside the document element. The root node does not have an expanded name.

B.1.2. Element Nodes

Each element in the original XML document is represented by an element node. The expanded name of the element is its local name, combined with any namespace that is in effect for the element. You can access the different parts of the element name with the name(), local-name(), and namespace-uri() functions. Here is an element from an XML document:

<xyz:report xmlns:xyz="">

The values of the three functions for this element node are:




B.1.3. Attribute Nodes

Attributes of elements in the XML document become XPath attribute nodes. An attribute has an expanded name, just as an element node has. The attribute nodes of a given element node are the attributes explicitly coded on the XML element and any attributes defined with default values in the DTD.

Taking a different approach from the Document Object Model, an element node is the parent of its attributes, although the attributes are not the children of the element. In other words, selecting all the children of an element node does not select any attribute nodes that the element node might have.

B.1.4. Text Nodes

Text nodes simply contain text from an element. If the original text in the XML document contained character or entity references, they are resolved before the XPath text node is created. Similarly, any existing CDATA sections appear as text nodes. You have no way of knowing if a given portion of a text node was originally a character or entity reference or a CDATA section.

B.1.5. Comment Nodes

A comment node is also very simple; it contains some text. Every comment in the source document (except for any comments in the DTD) becomes a comment node. The text of the comment node (returned with the text() node test) contains everything inside the comment except the opening <!-- and the closing -->.

B.1.6. Processing-Instruction Nodes

A processing-instruction node has two parts: a name (returned by the name() function) and a string value. The string value is everything after the name, including the whitespace, but not including the ?> that closes the processing instruction.

B.1.7. Namespace Nodes

Namespace nodes are almost never used in XSLT stylesheets; they exist primarily for the XSLT processor's benefit. One thing to keep in mind is that the declaration of a namespace (such as xmlns:auth=""), even though it is technically an attribute in the XML source, becomes a namespace node and not an attribute node. Namespace nodes exist for both the namespace prefixes that are defined and any default namespaces.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.