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 March 15th, 2010, 04:36 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default tokenize (counting and typing)

I have two questions related to tokenized values.


First question: I would like to grab the number of tokenized values using position() rather than count, however, the following code does not work:

XML input =
<el values="40.655 50.353 60.997 20.31"/>

XSL instructions =
<xsl:variable name="tokenizedVal" select="tokenize(@values,' ')" />
<xsl:variable name="valcount" select="$tokenizedVal[last()]/position()"/>

It does work when I use count(), but I was trying to avoid using it. Is there a way to use position() to return the number of tokenized values?



Second question: I would also like to assign an xs type to the $tokenizedVal variable, but I am receiving the following error message:

"Required item type of value variable $tokenizedVal is xs:float; supplied value has item type xs:string"

XSL instruction =
<xsl:variable name="tokenizedVal" select="tokenize(@values,' ')" as="xs:float" />

Is their a way to assign the datatype when assigning the variable?
 
Old March 15th, 2010, 04:50 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To solve the first question, I realized I should use index-of() instead of position() since my values are not nodes. The following seems to do the trick.

<xsl:variable name="valcount" select="index-of($tokenizedVal,$tokenizedVal[last()])"/>

I am still interested in suggestions for handling the second question.

Thanks!
 
Old March 15th, 2010, 05:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Why on earth would you want to write something as convoluted as index-of($tokenizedVal,$tokenizedVal[last()]) in place of count($tokenizedVal)? It's not even correct, except in the special case where all the tokens are different.

as="xs:float" asserts that the value is a float, it does not convert it to a float. To convert the sequence of strings to a sequence of floats, use

for $x in tokenize(...) return xs:float($x)

Do you really want float? It loses an awful lot of precision. Using xs:double is almost certainly a better choice.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 15th, 2010, 05:10 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Incidentally if you're really determined to avoid count(), you could do

($values/position())[last()]

but I really can't think of any reason why you would want to.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Tokenize JohnBampton XSLT 7 August 25th, 2009 07:34 AM
need some help with typing narmer93 Visual Studio 2005 1 May 20th, 2008 05:28 AM
tokenize bbvic XSLT 1 July 19th, 2007 04:10 PM
tokenize sakura C# 1 December 3rd, 2005 10:43 AM
Typing problem with the Phile DM2 BOOK: ASP.NET Website Programming Problem-Design-Solution 5 May 11th, 2005 06:21 AM





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