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

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

The important change here was adding the
as
attribute to declare the type. It's this that eliminates the need to create the document node. You can retain the use of

in the body of the

if you prefer, writing it as:


   

      

         

      

      

         

      

   


In this example above I have substituted

for

. In fact, either instruction would work. But using

creates a text node, only to extract its value and convert this to an integer, which is unnecessarily inefficient.

Beginners sometimes try to write this as:


    

       

    

    

       

    


This won't work, because when you get to the end tag of the

element, both variable declarations will have gone out of scope!

See Also


on page 425

xsl:when

The

element always appears as a child of

. It defines a condition to be tested and the action to be performed if the condition is true.

Changes in 2.0

None.

Format

  test = expression>

  


Position


is always a child element of

. There must be at least one

element within an

element.

Attributes

Name
Value
Meaning
test
mandatory
XPath Expression
The boolean condition to be tested

Content

A sequence constructor.

Effect

The

element is evaluated as follows:

  • The first

    element whose
    test
    expression has an effective boolean value of
    true
    is selected; subsequent

    elements are ignored regardless of the value of their
    test
    expression.
  • If none of the

    elements has a
    test
    expression that is
    true
    , the

    element is selected. If there is no

    instruction, no element is selected.
  • The sequence constructor contained in the selected child element (if any) is evaluated in the current context: that is, the effect is as if the relevant sequence constructor appeared in place of the

    instruction.

Any XPath expression may be evaluated to obtain an effective boolean value. The rules are given in the section for

on page 353.

The
test
expression in a

element after the selected one is not evaluated. This means it is safe to write code such as this:


  

    

  

  

    

  

  

    

  


The conversion to an integer will not be attempted, and cannot therefore cause a failure, if the condition in the first

is true.

Usage and Examples

See

on page 282

See Also


on page 282


on page 420


on page 353

xsl:with-param

The

element is used to set the values of parameters when calling a template, either when using

or when using

. It can also be used with

and with

.

Changes in 2.0

The

element can now be used as a child of

or

.

The
as
and
tunnel
attributes have been added.

When used with

, XSLT 2.0 defines compile-time errors if there is mismatch between the parameters supplied in the call and the parameters declared for the target

element.

Format

  name = qname

  select? = expression

  as? = sequence-type

  tunnel? = “yes” | “no”>

  


Position


is always a child of

,

,

, or

.

Attributes

Name
Value
Meaning
name
mandatory
Lexical QName
The name of the parameter.
select
optional
XPath Expression
The value of the parameter to be supplied to the called template.
as
optional
SequenceType
Defines the type of the value.
tunnel
optional
yes
or
no
Indicates whether this is a tunnel parameter.

Content

An optional sequence constructor. If a
select
attribute is present, the

element must be empty.

Effect

The

element assigns a value to a parameter. The value of the parameter can be used within the called template.

The value of the parameter is established in exactly the same way as for the

element. That is, the value is obtained by evaluating the
select
expression if present, or the contained sequence constructor if not.

Except for tunnel parameters (those where
tunnel=“yes”
is specified), the parameter value is available only to the immediately called template. If the called template has an

element whose name matches that of the

element, then the value assigned to the

element is available within the template. If the called template has no such parameter, the value is ignored, except when using

, when this is an error. In the case of

, the parameter value is available in each of the templates that is called (one per selected node). The parameter is effectively evaluated once only—it will have the same value for each of these templates.

Other books

The Ocean by Mia Castile
Keyboards and Kink by Danica Avet, Sandra Bunio, Vanessa Devereaux, Carolyn Rosewood, Melissa Hosack, Raven McAllan, Kassanna, Annalynne Russo, Ashlynn Monroe, Casey Moss, Xandra James, Jorja Lovett, Eve Meridian
Eggs with Legs by Judy Delton
Sea of Ink by Richard Weihe
No Country for Old Men by Cormac McCarthy