xsl:attribute
The xsl:attribute
element is used to add an attribute value to an xsl:element element or literal result element, or to an element
created using xsl:copy. The
attribute must be output immediately after the element, with no intervening
character data.
Category: instruction
Content: sequence-constructor
Permitted parent elements:
xsl:attribute-set
; any XSLT element whose content model is
sequence-constructor; any literal result element
Attributes
|
| Attribute name, interpreted as an attribute value template, so it may contain string expressions within curly braces. The full syntax of string expressions is outlined in XPath syntax. |
|
| The namespace URI of the attribute. |
|
| The attribute value may be given either by a
|
|
| String used to specify an alternative separator. |
|
| Indicates the data type of
the value of the attribute. The value must either be a built-in type defined in
XML Schema, for example |
|
| Invokes validation of the contents of the attribute, by locating a global schema declaration matching the name of the attribute. (Note that attributes often have no global declaration in the schema, in which case this option is not useful. It is more common to validate attributes implicitly by validating their parent element node.) |
Saxon availability
Available in XSLT 1.0 and later versions. Available in all Saxon editions. Available for all platforms.
Notes on the Saxon implementation
Motivated by streaming, the on-empty
attribute was introduced in an
early Working Draft for XSLT 3.0, but later removed and replaced by the new xsl:on-empty, xsl:on-non-empty and xsl:where-populated
instructions. The on-empty
attribute was implemented in Saxon 9.5,
but removed in 9.7.
Details
There are two main uses for the xsl:attribute
element:
-
It is the only way to set attributes on an element generated dynamically using xsl:element.
-
It allows attributes of a literal result element to be calculated using xsl:value-of.
The xsl:attribute
must be output immediately after the relevant
element is generated: there must be no intervening character data (other than
whitespace which is ignored). Saxon outputs the closing ">" of the element
start tag as soon as something other than an attribute is written to the output
stream, and rejects an attempt to output an attribute if there is no
currently-open start tag. Any special characters within the attribute value will
automatically be escaped (for example, "<" will be output as "<").
If two attributes are output with the same name, the second one takes precedence.
Examples
The following code creates a <FONT>
element with several
attributes: