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

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


  percy

  queenie

  rory


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

instructions before it merges this
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

or

element or in the
xsl:use-attribute-sets
attribute of a literal result element or, of course, in the
use-attribute-sets
attribute of another

. The first three cases all create an element node and have the effect of adding the attributes in the named attribute set to that element node. Any attributes added implicitly from a named attribute set can be overridden by attribute nodes added explicitly by the invoking code.

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

and

.

The rules for the scope of variables, described under

on page 500, are the same as anywhere else, and are defined by the position of the definitions in the source stylesheet document. This means that the only way to parameterize the values of attributes in a named attribute set is by reference to global variables and parameters: There is no other way of passing parameters to an attribute set. However, the value of the generated attributes may depend on the context in the source document. The context is not changed when the attribute set is used, so the context item (
.
) 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


elements:


   1

   3

   0

   100%


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:


   2

   cols


Then this new attribute set could also be invoked by name from a literal result element, an

instruction, or an

instruction.

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

elements in the poem output in the form

within the stanza.

Source

The source file
poem.xml
has the following structure (I'm only showing the first stanza):



Rupert Brooke

1912

Song


And suddenly the wind comes soft,

And Spring is here again;

And the hawthorn quickens with buds of green

And my heart with buds of pain.



Stylesheet

The stylesheet
number-lines.xsl
copies everything unchanged except the

elements, which are copied with a named attribute set:

 xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” version=“2.0”>




   

     

   



   

     

   



   

   



Output

The output (again showing only the first stanza) looks like this:


   Rupert Brooke

   1912

   Song

   

      And suddenly the wind comes soft,

      And Spring is here again;

      And the hawthorn quickens with

                      buds of green

      And my heart with buds of pain.

   


See Also


on page 306


on page 287

Literal Result Elements
in Chapter 3, page 112

xsl:call-template

The

instruction is used to invoke a named template. Its effect is analogous to a procedure call or subroutine call in other programming languages.

Changes in 2.0

There are no syntactic changes to this instruction in XSLT 2.0. However, using the

instruction in the called template (see page 452) now allows the result of the

instruction to be any sequence, not only a sequence of nodes.

In many cases where it was appropriate to use

in XSLT 1.0, it may be more appropriate in 2.0 to call a stylesheet function defined using

, which is described on page 344.

Many problems that required recursive use of

to process a string can be solved more conveniently in XSLT 2.0 by using the

instruction (see page 230), and many problems that used recursion to process a sequence of nodes can now be tackled more easily by using

, described on page 326.

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
version=“1.0”
.

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

Other books

Black Silk by Retha Powers
Such a Dance by Kate McMurray
The Man From Beijing by Henning Mankell
Lucky Strikes by Louis Bayard
How to Date an Alien by Magan Vernon