Subject: Re: [xsl] Variables From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Thu, 17 May 2001 14:43:01 +0100 |
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Variables, Mengel Andre (FV/SLM | Thread | Re: [xsl] Variables, Oliver Becker |
Re: [xsl] XSL to generate XPaths, Oliver Becker | Date | Re: [xsl] question about position(), Jeni Tennison |
Month |