Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
If there is more than one attribute set named
A1
these must be merged first (taking import precedence into account), and then the merged contents must be substituted into
B
.
Then the same process is applied to
A2
. The referenced attribute sets are expanded in order. The attributes that result from expanding
A1
are output before those that result from expanding
A2
. This means that attributes from
A2
take priority over those from
A1
if there is a clash, because of the rule that when several attributes have the same name, the last one wins.
In turn, the attribute set
B
must be fully expanded before it is merged with any other attribute set called
B
. That is, the processor must replace the references to attribute sets
A1
and
A2
with an equivalent list of
B
with other attribute sets of the same name.
When
B
is expanded, the attributes derived from
A1
and
A2
will be output before the attributes
p
,
q
, and
r
, so if expanding
A1
and
A2
generates any attributes called
p
,
q
, and
r
, these will be overwritten by the values specified within
B
(
percy
,
queenie
, and
rory
).
Normally when describing the processing model for XSLT instructions, we distinguish between the process of generating a sequence of nodes, and the subsequent process of attaching these nodes to a tree. Eliminating attribute nodes with duplicate names is technically part of the second process. However, attribute sets can only be expanded from instructions that create elements, so the resulting attributes will always be attached to an element. This means we can treat it as if generating the attribute nodes and attaching them to an element are done as a single process.
Duplicate attribute names or attribute set names do not cause an error. If several attributes have the same name, the one that comes last (in the order produced by the merging rules given above) will take precedence.
Usage
The most common use of attribute sets is to define packages of attributes that constitute a display style, for example a collection of attributes for a font or for a table. They are often used when generating XSL-FO.
A named attribute set is used by referring to it in the
use-attribute-sets
attribute of the
xsl:use-attribute-sets
attribute of a literal result element or, of course, in the
use-attribute-sets
attribute of another
An attribute set is not simply a textual macro. The attributes contained in the attribute set each have a
select
attribute or sequence constructor to define the value, and although this will often return a fixed value, it may also, for example, declare variables or invoke other XSLT instructions such as
The rules for the scope of variables, described under
.
) as well as the context position and size are exactly the same as in the calling instruction.
Examples
The following example defines an attribute set designed for generated HTML This attribute set can be used when generating an output element, as follows: This produces the following output: Alternatively it is possible to use the attribute set while overriding some of its definitions and adding others, for example: The output now becomes: If this combination of attributes is also used repeatedly, it could be defined as an attribute set in its own right, as: Then this new attribute set could also be invoked by name from a literal result element, an The next example shows that the values of the attributes in an attribute set need not be constants. Example: Using an Attribute Set for Numbering This is a rather untypical example, designed to show that attribute sets are more powerful than you might imagine. Suppose you want to copy an XML file containing a poem, but with the Source The source file Stylesheet The stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” version=“2.0”> Output The output (again showing only the first stanza) looks like this: buds of green See Also Literal Result Elements xsl:call-template The Changes in 2.0 There are no syntactic changes to this instruction in XSLT 2.0. However, using the In many cases where it was appropriate to use Many problems that required recursive use of It is now a compile-time error to supply a parameter that the called template does not declare; in XSLT 1.0, such a parameter was silently ignored. To preserve backward compatibility, this rule is not enforced when the stylesheet specifies
elements:… … … …
poem.xml
has the following structure (I'm only showing the first stanza):
number-lines.xsl
copies everything unchanged except the
in Chapter 3, page 112
version=“1.0”
.Other books