Selecting and Outputting Attributes
The XPath syntax for selecting an attribute from a source document is to prefix the @ symbol to the name of the attribute. For example, <xsl:value-of select="@exchange"/>
selects the value of an exchange
attribute defined for the current element. The example in this topic extracts the value of the exchange
attribute of <stock>
elements, and inserts it into the output. For more information about selecting attributes, see Using XPath Expressions to Select Nodes.
There are two ways to generate an attribute in the result tree. The first way is to add the attribute to an output element. For example, in portfolio-attributes.xsl, below, a BORDER
attribute is inserted directly into to the output <TABLE>
element. This way is useful if you know the attribute value when you write the style sheet.
Alternatively, you can use the <xsl:attribute>
instruction to create an attribute for any output element. This is useful when you must assign a data value from the source to the attribute at run time. For example, the following code fragment inserts a TITLE
attribute to the outgoing <TR>
element.
<TR> <xsl:attribute name="TITLE"><xsl:value-of select="symbol"/> is listed on the <xsl:value-of select="@exchange"/> stock exchange. </xsl:attribute> ..... </TR>
In this fragment, the attribute value is the content of the <xsl:attribute>
element — that is, whatever is enclosed between the <xsl:attribute> ... </xsl:attribute>
tags. For the stock with the ZCXM symbol in this example, the TITLE
attribute created above has a value of "ZCXM is listed on the nyse stock exchange"
. This produces the output <TR>
element, as follows:
<TR TITLE="ZCXM is listed on the nyse stock exchange"> ... </TR>
You can use both of the above methods to add attributes to a single element, even in a single rule or operation. Be aware, however, of the following limitations.
- You cannot add an attribute to an element that already has an attribute of that name.
- Attributes added with
<xsl:attribute>
must appear before any child elements are added to the element.
Example
This example uses an XML source file and an XSLT style sheet.
XML File (portfolio-attributes.xml)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="portfolio-attributes.xsl"?> <portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes" xml:space="preserve"> <stock exchange="nyse"> <name>zacx corp</name> <symbol>ZCXM</symbol> <price dt:dt="number">28.875</price> </stock> <stock exchange="nasdaq"> <name>zaffymat inc</name> <symbol>ZFFX</symbol> <price dt:dt="number">92.250</price> </stock> <stock exchange="nasdaq"> <name>zysmergy inc</name> <symbol>ZYSZ</symbol> <price dt:dt="number">20.313</price> </stock> </portfolio>
XSLT File (portfolio-attributes.xsl)
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<BODY>
<TABLE BORDER="2"
>
<TR>
<TD>Symbol</TD>
<TD>Name</TD>
<TD>Price</TD>
</TR>
<xsl:for-each select="portfolio/stock">
<TR>
<xsl:attribute name="TITLE"><xsl:value-of select="symbol"/>
is listed on the <xsl:value-of select="@exchange"/>
stock exchange.</xsl:attribute>
<TD><xsl:value-of select="symbol"/></TD>
<TD><xsl:value-of select="name"/></TD>
<TD><xsl:value-of select="price"/></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
Formatted Output