Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 28th, 2011, 06:10 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default the XPath 'for' statement, returns ???

I must be reading XPath 2.0 incorrectly.

It seems that the statement:
Code:
for $i in 1 to 10 return $i
would return a sequence of the numbers from 1 to 10.
But putting that in a variable:
Code:
<xsl:variable name="temp">
   <xsl:sequence select="for $i in 1 to 10 return $i"/>
<xsl:/variable>

<xsl:for-each select="$temp">
   <xsl:value-of select="."/>
</xsl:for-each>
is not giving me what I wanted: access to each of the members of the sequence created from 1 to 10.

Now, for my purpose, this may be moot as I may be able to use
Code:
<xsl:for-each select="1 to 10">
   <xsl:value-of select="."/>
</xsl:for-each>
and accomplish my goal. But I thought I was on to something with the results of the for/in/to/return statement being put in a variable.

Having it in a variable might be nice so I could pass the sequence around to other functions or templates.

Did I leave something out, or is it just the wrong application/purpose of the for/in/to/return construct?

Thanks,
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare

Last edited by mphare; January 28th, 2011 at 06:14 PM..
 
Old January 28th, 2011, 06:47 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Using <xsl:value-of> as a child of <xsl:variable> is a bad (and very common) coding habit. It often has the desired effect, but not always, and only at great expense. Using xsl:sequence is better, but still wrong unless you use the "as" attribute to indicate the required type. Most of the time, it should be replaced by use of the select attribute:

Code:
<xsl:variable name="var" select="for $i in 1 to 10 return $i"/>
which of course can be further simplified to

Code:
<xsl:variable name="var" select="1 to 10"/>
What's happening is that when xsl:variable has no select attribute, and contained instructions (and no "as" attribute) it creates a temporary XML document. Documents are text, so the sequence of integers is converted to a string "1 2 3 4 5 6 7 8 9 10" which is not at all what you want. If you need to use child instructions to construct the value you can say as="xs:integer*", which overrides the default behaviour of creating an XML document.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; January 28th, 2011 at 06:49 PM..
The Following User Says Thank You to mhkay For This Useful Post:
mphare (January 28th, 2011)
 
Old January 28th, 2011, 06:58 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Nice! I caught myself using value-of and changed it to sequence, but ti didn't change the result. Using the select statement gave me exactly what I was looking for.

Thanks!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare





Similar Threads
Thread Thread Starter Forum Replies Last Post
Radiobutton returns oguchi ASP.NET 4 General Discussion 2 August 18th, 2010 12:15 AM
XPath multiple conditional statement geek.shrek XSLT 2 January 5th, 2010 07:53 PM
using() in a method that returns.... belphoebes_friend General .NET 1 April 12th, 2007 04:41 PM
remove returns redruff Classic ASP Basics 13 January 9th, 2007 11:56 PM
UPDATE statement returns syntax error AviatorTim Classic ASP Databases 2 January 8th, 2004 05:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.