Sams Teach Yourself XML in 24 Hours | 2

Sams Teach Yourself XML in 24 Hours

You may want to keep a bookmark around for this page, as several of the XPath examples throughout the next section rely on the training log sample code.

Referencing Nodes

The most basic of all XPath patterns is the pattern that references the current node, which consists of a simple period:

.

If you're traversing a document tree, a period will obtain the current node. The current node pattern is therefore a relative pattern because it makes sense only in the context of a tree of data. As a contrast to the current pattern, which is relative, consider the pattern that is used to select the root node of a document. This pattern is known as the root pattern and consists of a single forward slash:

/

If you were to use a single forward slash in an expression for the training log sample document, it would refer to the trainlog element (line 4) because this element is the root element of the document. Because the root pattern directly references a specific location in a document (the root node), it is considered an absolute pattern. The root pattern is extremely important to XPath because it represents the starting point of any document's node tree.

As you know, XPath relies on the hierarchical nature of XML documents to reference nodes. The relationship between nodes in this type of hierarchy is best described as a familial relationship, which means that nodes can be described as parent, child, or sibling nodes, depending upon the context of the tree. For example, the root node is the parent of all nodes. Nodes might be parents of some nodes and siblings of others. To reference child nodes using XPath, you use the name of the child node as the pattern. So, in the training log example, you can reference a session element (line 6, for example) as a child of the root node by simply specifying the name of the element: session. Of course, this assumes that the root node (line 4) is the current context for the pattern, in which case a relative child path is okay. If the root node isn't the current context, you should fully specify the child path as /session. Notice in this case that the root pattern is combined with a child pattern to create an absolute path.

If there are child nodes there must also be parent nodes. To access a parent node, you must use two periods:

..

As an example, if the current context is one of the distance elements (line 15, for example) in the training log document, the .. parent pattern will reference the parent of the node, which is a session element (line 13). You can put patterns together to get more interesting results. For example, to address a sibling node, you must first go to the parent and then reference the sibling as a child. In other words, you use the parent pattern (..) followed by a forward slash (/) followed by the sibling node name, like this:

../duration

This pattern assumes that the context is one of the child elements of the session element (other than duration). Assuming this context, the ../duration pattern will reference the duration element (line 14) as a sibling node.

Thus far I've focused on referencing individual nodes. However, it's also possible to select multiple nodes. For example, you can select all of the child nodes (descendants) of a given node using the double slash pattern:

//

As an example, if the context is one of the session elements in the training log document (line 20, for example), you can select all of its child nodes by using double slashes. This results in the duration (line 21), distance (line 22), location (line 23), and comments (line 24) elements being selected.

Another way to select multiple nodes is to use the wildcard pattern, which is an asterisk:

*

The wildcard pattern selects all of the nodes in a given context. So, if the context was a session element and you used the pattern */distance, all of the distance elements in the document would be selected. This occurs because the wildcard pattern first results in all of the sibling session elements being selected, after which the selection is limited to the child distance elements.

To summarize, following are the primary building blocks used to reference nodes in XPath:

  • Current node—.

  • Root node—/

  • Parent node—..

  • Child node—Child

  • Sibling node—/Sibling

  • All child nodes—//

  • All nodes—*

These pattern building blocks form the core of XPath, but they don't tell the whole story. The next section explores attributes and subsets and how they are referenced.

Created: March 27, 2003
Revised: December 12, 2005

URL: http://webreference.com/programming/xml_24/1