[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Variables
> 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@xxxxxxxxxxxxxxxxxxxxxxx | | op|qo WWW: http://www.informatik.hu-berlin.de/~obecker | \-------------------------------------------------------------------/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|