This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Variables


Hi Mengel,

> 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 ?

The definition of xsl:value-of indicates that it evaluates the
expression in its select attribute, and converts that to a string in
the same way as the string() function (see Section 7.6.1 of the XSLT
Recommendation).

When you convert a node set to a string with the string function, you
always get the value of the *first* node in that node set (see Section
4.2 of the XPath Recommendation).

The XPath 'item[$n]' gets a node set of item elements that are
children of the current node. The item elements that are chosen depend
on the data type of the predicate. If the predicate were a number
(e.g. item[2]) then it would get the item element that had that
position amongst its sibling item elements (i.e. the second item
element).  Otherwise, the expression is converted to boolean in the
same way as the boolean() function (see Section 2.4 of the XPath
Recommendation).

In this case, and the point of the example, the variable $n is set to
a result tree fragment. Evaluating the variable gives a result tree
fragment, not a number, so it's converted to a boolean.  Converting
a result tree fragment to a boolean always results in true() because a
result tree fragment is treated, in this context, like a node set with
a single root node (which has children as defined by the content of
the variable).

As the predicate always evaluates to true, the node set returned by
'item[$n]' holds all the item children of the current node.  When this
node set is evaluated as a string, then you get the string value of
the first of the item elements.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]