8.1 XML Navigation Using XPath 177
//Chemical/*
- attribute. To select an attribute, use an@followed by the name of the
attribute. Wild cards can also be used for attributes. To obtain every at-
tribute of every Interview element, use this path:
//Interview/@*
- text. Text contained in an element can be explicitly selected by using the
text()function. This is not usually necessary. If an element only con-
tains text, then selecting the element will also select its text content.
One can navigate from one step to another in several ways. XPath calls this
theaxisof navigation. The most common are the following:
- child element. This is the normal way to navigate from one node to an-
other. If no other axis of navigation is explicitly specified, then the path
navigates to the child element. - descendant element. A double slash will navigate any number of levels
to a matching node. This is handy when one has a deep hierarchy. - parent element. One can go up one level by using a double dot. To obtain
the PubMed identifier (PMID) of every Chemical node, use this path:
//Chemical/../../PMID
- ancestor element. One can go up any number of levels by using the an-
cestor:: axis. For example, to obtain the PMID of every Chemical node,
even when it is located several levels above, use this path:
//Chemical/ancestor::PMID
- root element. A slash at the start of a path means to start at the highest
level of the document. If a slash is not specified at the start of a path,
then the path starts at the current element. This depends on the context in
which XPath is used.