9.2.2. Child Element Location Steps
The second
simplest location path is a single
element name. This path selects all child elements of the context
node with the specified name. For example, the XPath
profession refers to all
profession child elements of the
context node. Exactly which
elements these are depends on what the context node is, so this is a
relative XPath. For example, if the context node is the
Alan Turing
person element in Example 9-1,
then the location path profession refers to these
three profession child elements of that element:
<profession>computer scientist</profession>
<profession>mathematician</profession>
<profession>cryptographer</profession>
However, if the context node is the Richard Feynman
person element in Example 9-1,
then the XPath profession refers to its single
profession child element:
<profession>physicist</profession>
If the context node is the name child element of
Richard Feynman or Alan Turing's
person element, then this XPath
doesn't refer to anything at all because neither of
those has any profession child elements.
In XSLT, the context node for an XPath expression used in the
select attribute of
xsl:apply-templates and similar elements is the
node that is currently matched. For example, consider the simple
stylesheet in Example 9-2. In particular, look at
the template rule for the person element. The XSLT
processor will activate this rule twice, once for each
person node in the document. The first time the
context node is set to Alan Turing's
person element. The second time the context node
is set to Richard Feynman's
person element. When the same template is
instantiated with a different context node, the XPath expression in
<xsl:value-of
select="name"/> refers to a different element,
and the output produced is therefore different.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<xsl:apply-templates select="person"/>
</xsl:template>
<xsl:template match="person">
<xsl:value-of select="name"/>
</xsl:template>
</xsl:stylesheet>
When XPath is used in other systems, such as XPointer or XForms,
other means are provided for determining what the context node is.
9.2.3. Attribute Location Steps
Attributes
are also
part of XPath. To select a particular attribute of an element, use an
@ sign followed by the name of the
attribute you want. For example, the XPath expression
@born selects the born
attribute of the context node. Example 9-3
is a simple XSLT stylesheet that generates an HTML table of names and
birth and death dates from documents like Example 9-1.
Example 9-3. An XSLT stylesheet that uses root, child element, and attribute location steps
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates select="people"/>
</html>
</xsl:template>
<xsl:template match="people">
<table>
<xsl:apply-templates select="person"/>
</table>
</xsl:template>
<xsl:template match="person">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="@born"/></td>
<td><xsl:value-of select="@died"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
The stylesheet in Example 9-3 has three template
rules. The first template rule has a match pattern that matches the
root node, /. The XSLT processor activates this
template rule and sets the context node to the root node. Then it
outputs the start-tag <html>. This is
followed by an xsl:apply-templates element that
selects nodes matching the XPath expression
people. If the input document is Example 9-1, then there is exactly one such node, the root
element. This is selected and its template rule, the one with the
match pattern of people, is applied. The XSLT
processor sets the context node to the root people
element and then begins processing the people
template. It outputs a <table> start-tag and
then encounters an xsl:apply-templates element
that selects nodes matching the XPath expression
person. Two child elements of this context node
match the XPath expression person so
they're each processed in turn using the
person template rule. When it begins processing
each person element, the XSLT processor sets the
context node to that element. It outputs that
element's name child element
value and born and died
attribute values wrapped in a table row and three table cells. The
net result is:
<html>
<table>
<tr>
<td>
Alan
Turing
</td>
<td>1912</td>
<td>1954</td>
</tr>
<tr>
<td>
Richard
P
Feynman
</td>
<td>1918</td>
<td>1988</td>
</tr>
</table>
</html>