XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / attribute-set
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
The top-level element xsl:attribute-set defines and names attribute lists. The resulting attribute set, a sequence of attribute nodes, can be assigned as a whole to an output element.
Classification | Declaration |
Function group | Generating output |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:attribute-set is a top-level element directly inside the stylesheet’s root element. Any number of attribute sets may be defined, included or imported in the same stylesheet.
The element contains any number of xsl:attribute instructions, sensibly at least one. Other content, such as literal result elements, is forbidden.
Attributes:
Standard attributes apply. In addition, xsl:attribute-set has a required attribute name and an optional attribute use-attribute-sets. Neither supports attribute value templates.
name
Value | |
Usage | Required |
Introduction | XSLT 1.0 |
The name attribute identifies the generated attribute set. It must be a QName.
use-attribute-sets
Value | QNames (space-separated list) |
Usage | Optional |
Introduction | XSLT 1.0 |
The use-attribute-sets attribute imports other attribute sets into the one being defined. Its value is a list of defined attribute set QNames separated by whitespace (whitespace). Circular references are forbidden. If several imported sets have competing attributes of the same name, the first imported wins; the QName list order is decisive.
Note – Binding attribute sets to generated or copied elements
An attribute of the same name, use-attribute-sets, binds attribute sets created with xsl:attribute-set to element nodes in an output sequence. It can be used in xsl:copy, xsl:element or, as xsl:use-attribute-sets, in a literal result element.
Purpose:
Attribute sets let you centrally define and edit groups of frequently used attribute definitions. A common use is assigning presentation formatting, such as HTML or CSS styles.
An element in the result document may receive one or more attribute sets through the attribute use-attribute-sets. Its value is a space-separated list of attribute set names. On a literal result element, the corresponding prefix must identify the attribute as belonging to the XSL namespace: xsl:use-attribute-sets (a literal result element is never in the XSL namespace).
Attribute sets with the same name in one stylesheet are not an error. This can happen when stylesheets are combined by inclusion or import. Their attribute lists simply add together, even with different precedences.
This can produce duplicate names in the attribute-node sequence; they are not removed at this stage. Only during serialisation are all but the last duplicate discarded. The survivor therefore comes from the set defined last in document order, or the set with highest import precedence, which is evaluated last.
Sets from included stylesheets have the same precedence as local sets but are treated as appearing before them.
Examples:
Example 1 – Binding attribute sets to elements created with xsl:element:
<xsl:element name="ergebnis" use-attribute-sets="set1 set2 set3"/>
An attribute collection is assigned to a generated element through use-attribute-sets. Its token list names the attribute list definitions to use.
Example 2 – Binding attribute sets to elements copied with xsl:copy:
<xsl:template match="ergebnis">
<xsl:copy use-attribute-sets="set1 set2 set3">
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
The instruction xsl:copy must apply to elements, not the root node, attributes or similar nodes, to pass attribute sets.
Example 3 – Assigning attribute sets to literal result elements:
<ergebnis xsl:use-attribute-sets="set1 set2 set3"/>On a literal result element, the attribute use-attribute-sets must be marked as belonging to the XSL namespace by adding prefix xsl:; otherwise it is not recognised as an instruction and is copied to the result with the literal result element.
Example 4 – Combining two attribute sets of the same name:
a) No conflicting attribute definitions
<xsl:attribute-set name="set1">
<xsl:attribute name="att1">wert1</xsl:attribute>
<xsl:attribute name="att2">wert2</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="set1">
<xsl:attribute name="att2">wert3</xsl:attribute>
<xsl:attribute name="att4">wert4</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="/">
<ergebnis xsl:use-attribute-sets="set1"/>
</xsl:template>
Both sets share a name and are combined, assigning all their attributes to the element:
<ergebnis att1="wert1" att2="wert2" att3="wert3" att4="wert4"></ergebnis>
b) Two conflicting attribute definitions
<xsl:attribute-set name="set1">
<xsl:attribute name="att1">wert1</xsl:attribute>
<xsl:attribute name="att2">wert2</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="set1">
<xsl:attribute name="att2">anderer wert</xsl:attribute>
<xsl:attribute name="att4">wert4</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="/">
<ergebnis xsl:use-attribute-sets="set1"/>
</xsl:template>
Again, both sets are combined. The definition in the last set overwrites the same-named definition from an earlier set.
Attribute att2 therefore takes its value from the second set:
<ergebnis att1="wert1" att2="anderer wert" att4="wert4"></ergebnis>
Example 5 – Importing existing attribute sets into another:
<xsl:attribute-set name="gesamtset" use-attribute-sets="set1 set2">
<xsl:attribute name="att5">wert5</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="set1">
<xsl:attribute name="att1">wert1</xsl:attribute>
<xsl:attribute name="att2">wert2</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="set2">
<xsl:attribute name="att3">wert3</xsl:attribute>
<xsl:attribute name="att4">wert4</xsl:attribute>
</xsl:attribute-set>
The two existing sets are imported into attribute set gesamtset and can be assigned under that name to an element, together with its attribute definitions:
<xsl:element name="ergebnis" use-attribute-sets="gesamtset"/>Element definition:
XSLT 1.0:
<!-- Category: top-level-element -->
<xsl:attribute-set
name = qname
use-attribute-sets = qnames >
<!-- Content: xsl:attribute* -->
</xsl:attribute-set>
XSLT 2.0:
<!-- Category: declaration -->
<xsl:attribute-set
name = qname
use-attribute-sets? = qnames >
<!-- Content: xsl:attribute* -->
</xsl:attribute-set>
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 conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn