XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / attribute
(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 instruction xsl:attribute creates an attribute on an element just added to the result tree. The attribute name comes from instruction attribute name. Its value comes from the contained sequence constructor.
Classification | Instruction |
Function group | Creating result output |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted content:
xsl:attribute occurs inside a sequence constructor or template block in xsl:template creating an element, or inside an xsl:attribute-set definition.
An optional sequence constructor determines the value. In the simplest case, literal result content supplies it. Alternatively, the string value comes from attribute select.
Attributes:
Standard attributes apply. In XSLT 1.0, instruction xsl:attribute has required attribute name and optional attribute namespace. Both values can come from an attribute value template. XSLT 2.0 adds optional attributes select and, as mutually exclusive alternatives, validation and type.
name
Value | QName (also as an AVT) |
Usage | Required |
Introduction | XSLT 1.0 |
The name attribute supplies the created attribute’s name. This must be a qualified name and may have a namespace prefix whose namespace is given in element xsl:attribute.
namespace
Value | uri-reference (also as an AVT) |
Usage | Optional |
Introduction | XSLT 1.0 |
The namespace attribute supplies a namespace URI for the new attribute. The name attribute can specify a prefix; otherwise one is generated.
select
Value | xpath-expression |
Usage | Optional |
Introduction | XSLT 2.0 |
The XPath expression in attribute select determines the new attribute’s string value. Its result sequence becomes a string, considering optional attribute separator. With attribute select, the instruction must be empty and cannot also contain a sequence constructor.
Backwards-compatible mode uses only the first item as the string-value basis.
separator
Value | string (also as an AVT) |
Usage | Optional |
Introduction | XSLT 2.0 |
Attribute separator separates the items forming the effective attribute value, produced by select or a sequence constructor. The default is one space (#x20) for a sequence produced with attribute select, but an empty string for a sequence constructor.
type
Value | QName (of a schema type) |
Usage | Optional |
Introduction | XSLT 2.0 |
Optional, but mutually exclusive with attribute validation. Attribute type gives a schema type QName for validation. This can be a predefined (built-in) type or one from an imported schema. If validation succeeds, meaning property validity of the generated PSVI equals "valid", the type is associated with the attribute; otherwise transformation stops.
A QName referring to a complex type (complex type) is an error: attributes cannot have complex types.
validation
Value | "strict” | "lax” | "preserve” | "strip” |
Usage | Optional |
Introduction | XSLT 2.0 |
Optional, but mutually exclusive with attribute type. Attribute validation controls validation level. The default is "strip" unless element xsl:stylesheet specifies attribute default-validation with a different value.
The four explicit values are "strict", "lax", "preserve" and "strip". Their effects on xsl:attribute are:
validation="strict"
Value"strict"requires strict schema validation. The attribute must be declared in a schema available through standard XML Schema methods. Propertyvalidityin the attribute’s PSVI must equal"valid". If it equals"invalid"or"notKnown", transformation fails with a type error (type error).
validation="lax"
Value"lax"also validates against an available schema, but a missing schema or declaration is not an error. The PSVI’svalidityproperty can therefore equal"valid"or"notKnown". In the latter case, the unvalidated attribute receives typexs:untypedAtomic. If propertyvalidityequals"invalid", transformation fails with a type error (type error).
validation="preserve"
Value"preserve"normally preserves an explicitly assigned type. For newly created attributes this is meaningless, so"preserve"has the same effect as"strip", assigning typexs:untypedAtomicto the attribute.
validation="strip"
Value"strip"assigns the created attribute typexs:untypedAtomic. (Note: Normally this removes any existing type association, but forxsl:attributeno prior association can exist.)
Purpose:
The instruction xsl:attribute relates to an element appearing immediately before it in the result sequence, allowing only intervening attribute or namespace nodes. That element may be created by xsl:copy, xsl:element or a literal result element.
The attribute must precede every child, including whitespace text. The element is then said to be open. This XSLT 1.0 rule enables immediate node-by-node serialization.
A new attribute replaces a preceding attribute with the same QName, as may occur with attribute sets.
Its value comes from attribute select or the optional template body inside xsl:attribute. This may be literal text or XSLT instructions producing strings, such as xsl:value-of. Non-text results from the constructor are an error. Without either attribute select or a constructor, the attribute receives an empty string.
Alternatives to xsl:attribute:
Instead of xsl:attribute, an AVT in a literal result element is simpler, but lacks immediate validation or type assignment. Compare these alternatives, using value from current-node attribute wert:
1. With instruction xsl:attribute:
<ergebnis>
<xsl:attribute name="testattribut">
<xsl:value-of select="@wert"/>
</xsl:attribute>
</ergebnis>
2. With an AVT:
<ergebnis testattribut="{@wert}"/>
An AVT is often a useful shorter alternative to xsl:attribute when the name is fixed and immediate validation unnecessary. A runtime name or required validation needs the instruction instead.
Examples
Example 1 – with xsl:element:
This block
<xsl:element name="ergebnis">
<xsl:attribute name="testattribut">testwert</xsl:attribute>
</xsl:element>
produces:
<ergebnis testattribut="testwert"></ergebnis>
Example 2 – an AVT:
An AVT determines the name, here by reading a variable:
<xsl:variable name="att-name">testattribut</xsl:variable>
<ergebnis>
<xsl:attribute name="{$att-name}">testwert</xsl:attribute>
</ergebnis>
Example 3 – with a namespace:
a) Namespaced attribute with a prefix:
<ergebnis>
<xsl:attribute name="beispiel:test" namespace="http://www.beispiel.de">testwert</xsl:attribute>
</ergebnis>
The XSLT processor recognizes the colon-separated part as the namespace prefix, here using Xalan:
<ergebnis beispiel:test="testwert" xmlns:beispiel="http://www.beispiel.de"/>
b) Namespaced attribute without a prefix:
<ergebnis>
<xsl:attribute name="test" namespace="http://www.beispiel.de">testwert</xsl:attribute>
</ergebnis>
The processor generates an arbitrary prefix, again using Xalan:
<ergebnis ns0:test="testwert" xmlns:ns0="http://www.beispiel.de"/>
Example 4 – resolving name conflicts:
If a same-named attribute already exists, including the same namespace, xsl:attribute replaces its first value:
<ergebnis testattribut="dies wird überschrieben">
<xsl:attribute name="testattribut">testwert</xsl:attribute>
</ergebnis>
produces:
<ergebnis testattribut="testwert"></ergebnis>This intentionally raises no error and is useful for attribute sets assigned together. Combining sets may create duplicates handled this way (see xsl:attribute-set).
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:attribute
name = { qname }
namespace = { uri-reference }>
<!-- Content: template -->
</xsl:attribute>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:attribute
name = { qname }
namespace? = { uri-reference }
select? = expression
separator? = { string }
type? = qname
validation? = "strict" | "lax" | "preserve" | "strip">
<!-- Content: sequence-constructor -->
</xsl:attribute>
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