Stylesheets can calculate attributes using xsl:attribute. Where
xsl:value-of creates text, xsl:attribute creates attributes. The contents of
the xsl:attribute element will become the value of the attribute. This
differs from xsl:value-of which uses a select pattern to create text.
Typically, the contents of an xsl:attribute will be a combination of
text and xsl:value-of elements.
The following example creates a useful tag, the <box> tag.
The contents of the tag are placed in an HTML table to format a box. The
CSS style of the table is specified by an attribute in the source XTP. Tags
like <box> are very useful examples of XSL. Instead of hard-coding HTML
tables in a JSP file, the page can specify boxes. If the site wants to
change the look, it's simple to change the stylesheet and modify the entire
site at once.
default.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="box">
<table>
<xsl:attribute name="class">
<xsl:value-of select="@class"/>
</xsl:attribute>
<tr>
<td>
<xsl:apply-templates/>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
|
As mentioned above, the xsl:attribute uses xsl:value-of to compute the
value of the @class attribute. The results below shows how much
easier the XTP file will be when you use a <box> tag instead of creating
the table directly.
hello.xtp
<box class="example">
This is an example.
</box>
|
<table class="example">
<tr>
<td>This is an example</td>
</tr>
</table>
|
The StyleScript equivalent for xsl:attribute uses name as its
primary argument. The value of the attribute is defined
within <<...>> tags as usual. The stylesheet shows how
to combine text with the value-of expression.
default.xsl
$template(example) <<
<table>
$attribute("class") <<$(@class)>>
<tr>
<td>
$apply-templates();
</td>
</tr>
</table>
>>
|
- Tags in stylesheets are parsed, not raw text. So you can add attributes
to elements.
- xsl:attribute name="my-name"> adds an attribute to the
current element.
- $attribute(my-name) expands to xsl:attribute name="my-name".
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|