Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The following example outputs a
code
or
reason-code
attribute, depending on the variable
$schema-version
. This kind of logic can be useful in an application that has to handle different versions of the output document schema.
Example: Deciding the Attribute Name at Runtime
This example shows the use of
Source
This example uses no source file; it can be run by specifying
main
as the name of the initial template.
Stylesheet
The stylesheet can be found in the file
conditional.xsl
.
The stylesheet declares a global parameter
schema-version
, which controls the name of the attribute used in the output file.
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
select=“if ($schema-version lt 3.0)
then ‘code’
else ‘reason-code”’/>
Output
With the default value of the parameter
schema-version
, the output is:
When run with the parameter
schema-version
set to 2.0, the output is:
In XSLT 1.0, it was often necessary to use
content
attribute is a whitespace-separated list of the
id
attributes of the child
In XSLT 2.0, the work can often be done at the XPath level. This example can now be written:
because when the value of an expression in an attribute value template is given as a sequence, the processor will automatically convert the items in this sequence to strings and concatenate them using a single space as a separator.
See Also
xsl:attribute-set
The
Changes in 2.0
None.
Format
name = qname
use-attribute-sets? = qnames>
Position
Attributes
Name | Value | Meaning |
name mandatory | Lexical QName | The name of the attribute set |
use-attribute-sets optional | Whitespace-separated list of lexical QNames | The names of other attribute sets to be incorporated into this attribute set |
Content
Zero or more
Effect
Named attribute sets provide a capability similar to named styles in CSS.
The
name
attribute is mandatory and defines the name of the attribute set. It must be a lexical QName: a name with or without a namespace prefix. If the name uses a prefix, it must refer to a namespace declaration that is in scope at this point in the stylesheet, and as usual it is the namespace URI rather than the prefix that is used when matching names. The name does not need to be unique; if there are several attribute sets with the same name, they are effectively merged.
The
use-attribute-sets
attribute is optional. It is used to build up one attribute set from a number of others. If present, its value must be a whitespace-separated list of tokens each of which is a valid lexical QName that refers to another named attribute set in the stylesheet. For example:
use-attribute-sets=“small-font gray-background centered”/>
The references must not be circular: if A refers to B, then B must not refer directly or indirectly to A. The order is significant: specifying a list of named attribute sets is equivalent to copying the
beginning
of the list of
If several attribute sets have the same name, they are merged. If this merging finds two attributes with the same name, then the one in the attribute set with higher import precedence will take precedence. Import precedence is discussed under
The order in which this merging process takes place can affect the outcome. When
use-attribute-sets
appears on an
xsl:use-attribute-sets
on a literal result element, it is expanded to create a sequence of attribute nodes. This is essentially done by a recursive process:
1.
To expand an
[xsl:]use-attribute-sets
attribute, use rule 2 to process each of the attribute set names in the order they are listed.
2.
To process an attribute set name, use rule 3 to expand each of the
3.
To expand an
use-attribute-sets
attribute (if any), then add the attribute nodes generated by evaluating the contained
It's best to illustrate this by an example. Suppose you have the following attribute-set definition: