home—lects—exams—hws
D2L—breeze (snow day)
lect13a-xpath-ch03
XPATH
ch03
Originally based on
XML Visual Quickstart Guide
by Kevin Howard Goldberg,
and notes therefrom by Jack Davis (jcdavis@radford.edu).
In the last chapter XSL templates were described and examined. When you apply a template,
you use an expression to specify the node set that should be processed. You write
both patterns and expressions using XML Path Language (XPath) syntax.
XPath is a language for selecting nodes and node sets by specifying their location
paths in the XML document. This chapter will describe how to specify XPath location
paths in detail. XPath can be used in XSLT instructions to further process given node sets to
return values instead of nodes. XPath has built-in functions to do math, process strings,
and test conditions in an XML document. The next chapter, XPath Functions, will
describe these functions.
- Locating Nodes -
XPath location paths to refer to a node or node set. Remember that a node is an
individual piece of the XML document (such as an element, an attribute, or some text content).
A location path uses relationships to describe the location of a node or set of nodes relative
to a given node. When translating location paths, XPath considers all XML documents as
tree structures.
Specifically, they are considered node trees, which are a hierarchical structure of nodes.
more example nodes
- XML Node Tree --
In an XML node tree, everything in the tree is a node, and every node is in some way related to
another node in the tree. At the top of the node tree is the root node. The root node,
or document node, can have any number of child nodes. To these child nodes, the root node is
the parent node. These child nodes can have any number of child nodes themselves, and so on,
and so on. Child nodes with the same parent are called sibling nodes. Descendent nodes are a
node's child nodes, its children's child nodes, and so forth. Ancestor nodes are a node's parent
node, grandparent nodes, etc. Through XPath location paths, you can access any of these nodes
from any other simply by knowing the relationship between the two.
- Location Paths --
- relative location path -
consists of a sequence of location steps separated by / (a forward slash).
Each step selects a node or node set relative to the current node. Then,
each node in that set is used as the current node for the following step,
and so on.
- absolute location path -
consists of / (a forward slash), optionally followed by a relative location path.
A / by itself selects the root node of the XML document. If it is by a relative location
path, then the location path is a relative location path starting at the root node.
The choice of whether to use a relative or an absolute location path depends on the
circumstance. Relative location paths are most commonly used, because they generate
the resulting node set relative to the current node.
-
Working on a subset of nodes: The history of each wonder:
all wonders: summary output
all wonders xsl
- In XPath, there are seven different node types:
-
root nodes,
-
element nodes (tags)
-
text nodes,
-
attribute nodes,
-
comment nodes,
-
processing instruction nodes,
and
-
namespace nodes.
For each node tyhpe, there is a way of retrieving its value.
For some, the value is part of the node itself;
for others, the value is based on the value of its descendant nodes.
- Building XPath location paths --
- . - refers to the context or current node
- .. - refers to the parent of the current node
- ./* - select all of the children of the current node
- location path to the node; - /@ refers to all the current node's attributes
- Sometimes you don't want all children nodes, but
just a subset of them.
Use square-brackets ([, ])
to filter the current node set:
/* the name of every wonder, but only those with a language
* attribute equal to 'English'*/
/ancient_wonders/wonder/name[@language='English']
/* the name node of the last wonder. */
/ancient_wonders/wonder[position()==last()]/name
|
Predicates can compare values, test for existence,
do math, and more.
You use XPath Functions (next chapter)
to create more complex conditions.
- A double-slash selects all descendents:
.// - select all descendants of the current node
— no matter how deeply nested!
- //elementName will output all the matching elements in the
document whose name
is elementName, wherever they may be.
-
An example where the xpath refers to an individual attribute,
not even a full html-tag:
lect13a-xpath-ch03-20.xml
lect13a-xpath-ch03-21.xsl
home—lects—exams—hws
D2L—breeze (snow day)