Attribute Location Steps
Attributes are also addressable by 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.
<?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 ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.