This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Variables
- To: xsl-list at lists dot mulberrytech dot com
- Subject: Re: [xsl] Variables
- From: Oliver Becker <obecker at informatik dot hu-berlin dot de>
- Date: Thu, 17 May 2001 16:11:27 +0200 (MET DST)
- Reply-To: xsl-list at lists dot mulberrytech dot com
> Could someone explain me the following example of the W3C specification
>
> <xsl:variable name="n">2</xsl:variable>
> ...
> <xsl:value-of select="item[$n]"/>
>
> This will output the value of the first item element, because the variable n
> will be bound to a result tree fragment, not a number.
> (See chapter 11.2)
>
> What is the reason for outputing the value of only the first item element ?
You are referring to http://www.w3.org/TR/xslt#variable-values
Ok, step by step:
item[$n] contains a predicate: [$n], whose expression is $n, which
in turn is a result tree fragment.
Looking at http://www.w3.org/TR/xpath#predicates
you'll find the following:
"... If the result [of evaluating the PredicateExpr (my annotation)] is a
number, the result will be converted to true if the number is equal to the
context position and will be converted to false otherwise; if the result is
not a number, then the result will be converted as if by a call to the
boolean function."
$n isn't a number (it's a rtf), so it is converted to a boolean, i.e.
to true (the rtf isn't empty).
item[true()] then selects all the item nodes, it is a node-set.
xsl:value-of select="item[$n]" outputs the string value of the
select expression: http://www.w3.org/TR/xslt#value-of
It does this by using the string function.
The string function with a node-set as argument returns the string value
of the first node in document order: http://www.w3.org/TR/xpath#function-string
In the end what you get is the value of first item element.
Questions?
I think in addition the work-arounds given in the XSLT spec, it's also
possible to write: item[number($n)]
Cheers,
Oliver
/-------------------------------------------------------------------\
| ob|do Dipl.Inf. Oliver Becker |
| --+-- E-Mail: obecker@informatik.hu-berlin.de |
| op|qo WWW: http://www.informatik.hu-berlin.de/~obecker |
\-------------------------------------------------------------------/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list