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 April 23rd, 2009, 09:11 AM
Registered User
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 1000 delimiter trouble


I'm working with SharePoint Designer and I have a webpart based on a document library that I have converted to a xslt dataview.

What I'm trying to do is that I need to access a numeric value in my webpart so I can perform calulations with it in JavaScript. The value will always be a 5 digit value, e.g. "14 104". Now, notice that the 1000 delimiter is a space (I'm working with swedish regional settings, english versions uses a comma i.e. 14,104). I need to remove that delimiter to perform calulations.

This is what I tried first:

<xsl:variable name="NbDaysLeft"  select="number(translate(@Dagar_x0020_kvar, ' ', '')" />
    <script type="text/javascript">
    //Fetch value from the xsl variable
    var DaysToDue = &apos;<xsl:value-of disable-output-escaping="no" select="$NbDaysLeft" />&apos;;
... ... ...
If I then try to print DaysToDue I get NaN (Not a Number) returned. And if I leave out the number() function call and print the value I still get "14 104" indicating that the translate failed or isn't applicable in this case.

So I then tried to format the value in JavaScript, here's my current code:

    <xsl:variable name="NbDaysLeft"  select="@Dagar_x0020_kvar" />
    <script type="text/javascript">
    //Fetch value from the xsl variable
    var DaysToDue = &apos;<xsl:value-of disable-output-escaping="no" select="$NbDaysLeft" />&apos;;
    var newDaysToDue;
    var validChars = &quot;0123456789&quot;;

    for(var i = 0; i &lt; DaysToDue.length-1; i++)
     if(validChars.indexOf(DaysToDue.substring(i, 1)) != -1) /*DaysToDue[i] doesn't work. Printing DaysToDue[i] returns "undefined".*/
      alert(DaysToDue.substring(i, 1) + &quot; &quot; + &quot;matched&quot;);
      //Move valid char to a new variable.
      newDaysToDue += DaysToDue.substring(i, 1);
     alert(&quot;Updated value: &quot; + newDaysToDue);
    newDaysToDue = &quot;&quot;;
Output from the first alert:
Iteration : Output
1 - "1 matched"
2 - " matched"
3 - "4 matched"

Output from the second alert:
Iteration - Output
1 - "Updated value: undefined1"
2 - "Updated value: undefined1"
3 - "Updated value: undefined14"
4 - "Updated value: undefined14"
5 - "Updated value: undefined14"

As you can see the code successfully finds the first digit. But then spooky things starts to happen. Where does it find this "undefined" character? Why is it inserted before 1? And why does the matchings fail after 4?

Just to narrow it down I created a local variable initiated to "14 104", removed the space with a .split.join, and parsed it to an integer. Printing the value resultet in "14104" and I also validated that it was in fact a number by running it through typeof().

The idea behind all this can be found in this blog if it helps as reference: http://pathtosharepoint.wordpress.com/2008/08/25/a-countdown-for-tasks-lists/'

Any input is greatly appriciated!
Old April 24th, 2009, 04:10 AM
Friend of Wrox
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts

Try changing
<xsl:variable name="NbDaysLeft" select="number(translate(@Dagar_x0020_kvar, ' ', '')"/>

<xsl:variable name="NbDaysLeft" select="number(translate(@Dagar_x0020_kvar, ' ', ''))"/>

You have missed a closing paranthesis for number function. If you use a good xslt editor, you could have easily found this error.
Old April 24th, 2009, 04:38 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

>You have missed a closing paranthesis for number function.

I find it hard to believe that the code would have executed at all with that error. But this does suggest that the code that was posted is not the code that was actually used, which makes it tricky to find the real bug.

My guess would be that the space in the data is not a space character at all, but a NBSP. Only a guess, mind you.
Michael Kay
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Old April 24th, 2009, 04:45 AM
Registered User
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Sorry, that was a silly mistake. I use SharePoint designer as my editor, and it doesn't support xslt.

However, my problem still remains. I made a quick edit in my code for this thread and unfortunatly missed one parethesis, the original code is correct, so I still get "NaN" as a respond.

A little update on this issue: To make sure this in fact is a problem related to the 1000 delimiter, I tried running the code with numbers below 1000 instead, and they were correctly printed in my alert.

EDIT: Thanks for your reply Michael, I've suspected that this "space" character isn't what I've thought it to be. But I didn't come to think of the nbsp entity. I tried using it in various forms in my code, but all of my attempts made the code to not run at all. I'll check on how to write this character so my xslt knows what I mean. Cause simply writing...

<xsl:variable name="NbDaysLeft" select="number(translate(@Dagar_x0020_kvar, '&nbsp;', '')"/>
... did not work.

EDIT#2: This did the trick:

<xsl:variable name="NbDaysLeft"  select="number(translate(@Dagar_x0020_kvar, ' ', ''))" />
Many thanks Michael! I had worked out another mean of performing the same task, but it was quite rough on the system and in no ways as elegant and robust as this one. Also, I don't think the above code will show my solution, the space is in fact written as '&#160' with an ending semicolon.

Last edited by PepperX; April 24th, 2009 at 05:20 AM..

Similar Threads
Thread Thread Starter Forum Replies Last Post
Comma Delimiter / Text Qualifier pearce64 Excel VBA 0 October 25th, 2005 04:57 PM
Multiple delimiter Text file import into Access Suomi Access VBA 2 June 12th, 2005 10:26 AM
Chaper 14 - over $1000 problems ftb_tx BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 November 21st, 2004 09:15 PM
Delimiter for loop that may have blank rows kwerle XSLT 2 November 26th, 2003 01:25 PM

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