XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Creating nodes / Collections of attribute declarations – xsl:attribute-set
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 2.)
The declaration xsl:attribute-set defines a named attribute set: a collection of attributes that can be assigned together to a target element. Inside xsl:attribute-set there are therefore one or more xsl:attribute instructions. The name attribute names the set, allowing a target element to reference it by name through an attribute use-attribute-set.
Advantages of attribute sets
An attribute set ensures consistent attributes across all elements using that collection. It is defined only once, reduces repetitive source code, and is easy to extend:
<xsl:attribute-set name="set_name">
<xsl:attribute name="attr_name">
<!-- Sequenzkonstruktor für Attributwert -->
</xsl:attribute>
<xsl:attribute name="anderer_attr_name">
<!-- Sequenzkonstruktor für Attributwert -->
</xsl:attribute> ...
</xsl:attribute-set>
The following example declares an attribute set named "bildstyle" and uses it in a templaterule creating an HTML-<img>:
<xsl:attribute-set name="bildstyle">
<xsl:attribute name="hspace">10</xsl:attribute>
<xsl:attribute name="vspace">5</xsl:attribute>
<xsl:attribute name="align">left</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="bild">
<img xsl:use-attribute-sets="bildstyle">
<xsl:attribute name="src">
<xsl:value-of select="unparsed-entity-uri(@quelle)"/>
</xsl:attribute>
</img>
</xsl:template>
The use-attribute-set attribute in the literal result element <img> requires the prefix xsl:, because it must be interpreted as an XSLT attribute rather than copied. An attribute value template is not allowed here.
In the result document:
...
<img src="mein_bild.gif" align="left" hspace="10" vspace="5"/>
...
Instead of creating new nodes, it is often enough to copy them. XSLT offers two basic ways to do this.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from "XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn