XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Runtime attribute values: attribute value templates
In XSLT, a “template” resembles a function block in a programming language: a sequence of instructions executed when the template is invoked. The everyday sense of a reusable layout or matrix is misleading here.
Within a template block, XSLT instructions are interpreted and literal result elements copied unchanged into the result. Some flexibility for the latter would sometimes help.
Attribute values and their origins
Literal result elements can have attributes, but their values are initially literal too: the desired value must be known in advance to write it into the stylesheet.
<xsl:template match="abschnitt[@typ='wichtig']">
<p class="wichtig"><xsl:value-of select="."/></p>
</xsl:template>
This rule processes only <abschnitt> elements whose typ attribute has value "wichtig" and sets a corresponding class attribute to assign a CSS style. Each class would require its own template.
XPath expressions as attribute values
The XSLT parser evaluates XPath expressions and acts on their results, inserting a string or constructing a sequence. It only evaluates XPath where expected, however: in the appropriate attributes of XSLT elements.
Attributes of literal result elements, and some XSLT instructions, often need values determined from the processing context at runtime. Predicting the value or providing every static configuration may be impossible.
The solution is called an attribute value template (attribute value template, AVT).
The attribute value template
To make the XSLT processor recognize and evaluate an XPath expression inside a literal result attribute, control characters are needed: braces { and }.
<elementname attributname="{expression}">
Attribute values must be strings, so XPath expressions in AVTs are interpreted as strings or converted to strings if needed.
The earlier problem now has a simple solution:
<xsl:template match="abschnitt">
<p class="{@typ}"><xsl:value-of select="."/></p>
</xsl:template>
Instead of filtering the node sequence across static templates, an AVT extracts the current node’s typ attribute’s string value and writes it as the output class attribute.
Embedded XPath expressions
XPath expressions can be embedded in fixed text, or several expressions can produce one attribute value. Mark every evaluated subexpression with braces:
<elementname attributname="{exp1}literal{exp2}">
Similarly, function position() can generate an identifier-like value in HTML:
<xsl:template match="abschnitt">
<p id="teil{fn:position()}"><xsl:value-of select ="."/></p>
</xsl:template>
For example:
<p id="teil3">Der Inhalt des dritten Abschnitts</p>Passing attribute values on
A source attribute often supplies a corresponding, not necessarily same-named, result attribute. For an HTML image retaining dimensions from XML, use:
<bild quelle="katze.gif" breite="200" hoehe="120"/>should produce:
<img src="katze.gif" width="200" height="120"/>Use this template:
<xsl:template match="bild">
<img src="{@quelle}" width="{@breite}" height="{@hoehe}"/>
</xsl:template>
Converting current-node element content into attribute values
Element content may also need to become a result attribute. Suppose <bild> is not empty but contains a description destined for an alt attribute. For example:
<bild quelle="katze.gif" breite="200" hoehe="120">Ein Bild meiner Katze</bild>
should become:
<img src="katze.gif" width="200" height="120" alt="Ein Bild meiner Katze"/>
Extend the template as follows:
<xsl:template match="bild">
<img src="{@quelle}" width="{@breite}" height="{@hoehe}"alt="{.}"/>
</xsl:template>
Here, "{.}" extracts the element’s string value, safer if child elements exist.
Copying child-element content into attributes
AVTs also work when information lies in child-element content rather than current-node attributes. The input:
<bild>
<quelle>katze.gif</quelle>
<breite>200</breite>
<hoehe>120</hoehe>
</bild>
becomes the structure shown earlier through:
<xsl:template match="bild">
<img src="{quelle}" width="{breite}" height="{hoehe}"/>
</xsl:template>
An AVT always returns the XPath expression’s string value, here a NameTest selecting children by element name alone. There is no need to consider the data source’s potential internal structure.
Escaping braces
A similar problem occurs with <, > or & when a brace must be a literal attribute character.
Since braces are control characters here, escape one by doubling it with another facing the same way. Thus { is written as {{.
<tag att="Dies ist {{kein}} Template">
corresponds to the intended content:
<tag att="Dies ist {kein} Template">
Note:
Attributes that prohibit AVTs have the same issue: the parser interprets braces as an attempted AVT and rejects it. Escape braces there too. Replacing one with a character entity { or } also fails because the preceding XML parser expands the entity to the unwanted control character before XSLT processing begins.
Where are AVTs allowed?
AVTs cannot be used everywhere. They are generally allowed in literal result attributes unless those attributes belong to the xml or xsl-namespace, such as xml:lang, xsl:xpath-default-namespace and others.
XSLT instructions also restrict AVTs to a precisely specified set of attributes.
Here is an overview:
XSLT instruction | Attributes allowing AVTs |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Table: AVTs allowed in XSLT 2.0 instructions.
All literal result attributes generally permit AVTs. XSLT instructions restrict them to the attributes listed above.
Warning – changes from XSLT 1.0
XSLT 2.0 allows AVTs for some attributes previously disallowed, such as attribute terminate on xsl:message. The newly added collation attribute also always permits AVTs, except on xsl:key.
Why these restrictions?
A missing runtime attribute value must not jeopardize stylesheet processing in critical cases. Consequently, AVTs are forbidden on all top-level XSLT elements. Thus attribute collation on xsl:key is not an AVT, unlike its use on other instructions.
Otherwise, static errors might only emerge at runtime. Stylesheet processing proceeds in order: the processor cannot go back when a needed value becomes available later, but fails first. Similar restrictions apply to XSLT parameters and variables.
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