XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (139 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
2.25Mb size Format: txt, pdf, ePub
ads

The following example outputs a

element with either 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

in a situation where the name of the generated attribute is decided at runtime.

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

because the value required a calculation. In the following example, the value of the
content
attribute is a whitespace-separated list of the
id
attributes of the child

elements of the context node:


   

      

         

         

             

         

      

   


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


on page 306


on page 287


on page 292

xsl:attribute-set

The

element is a top-level XSLT declaration used to define a named set of attribute names and values. The resulting attribute set can be applied as a whole to any output element, providing a way of defining commonly used sets of attributes in a single place.

Changes in 2.0

None.

Format

  name = qname

  use-attribute-sets? = qnames>

  


Position


is a declaration, so it must always occur as a child of the

element.

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

elements.

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”/>


   Verdana

   6pt



   #xBBBBBB



   center


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

elements that they contain, in order, to the
beginning
of the list of

elements contained in this

element.

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

on page 357. If they both have the same precedence, the one that came later in the stylesheet is used.

The order in which this merging process takes place can affect the outcome. When
use-attribute-sets
appears on an

or

element, or
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

declarations having that name, taking them in increasing order of import precedence, and within each import precedence, in declaration order. For definitions of import precedence and declaration order, see

on page 357.

3.
To expand an

declaration, use rule 1 to expand its
use-attribute-sets
attribute (if any), then add the attribute nodes generated by evaluating the contained

instructions to the result sequence.

It's best to illustrate this by an example. Suppose you have the following attribute-set definition:

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
2.25Mb size Format: txt, pdf, ePub
ads

Other books

Love Emerged by Michelle Lynn
Pretty Persuasion by Olivia Kingsley
In Real Life by Chris Killen
Dawn of Ash by Rebecca Ethington
Virgile's Vineyard by Patrick Moon
The Drake House by Kelly Moran
Drunk Mom by Jowita Bydlowska