Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Laurence Gillian 600 posts 1219 karma points
    Jun 11, 2010 @ 20:22
    Laurence Gillian
    0

    Splitting a Number

    Hi guys,

    Was wondering if anybody could help me with this, its a bit of an odd one!

    I want to split a value, in to its separate elements. So 123, would become 123. 

    Somethings like...

    <xsl:variable name="value" select="123">
    
    <xsl:variable name="valueTransform" select="magic-transform($123)" />
    
    which would give an output like
    
    <number>
        <node>1</node>
        <node>2</node>
        <node>3</node>
    </number>

    The rational behind this, is splitting numbers so they can each be assigned a css-class ready for image replacement.

    So the number becomes a pretty image, but its still accessible!

    Any pointers much appreciated! Laurie 

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 11, 2010 @ 20:26
    Lee Kelleher
    1

    Hi Laurence,

    Do you have control over the value "123"? Could you comma-separate them? "1,2,3"?

    If so, then you could use umbraco.library:Split() to do what you need.

    Cheers, Lee.

  • Laurence Gillian 600 posts 1219 karma points
    Jun 11, 2010 @ 21:01
    Laurence Gillian
    0

    No, sadly not its a two digit week number. /L (so ranges from 0 - 52)

  • Tommy Poulsen 514 posts 708 karma points
    Jun 11, 2010 @ 21:20
    Tommy Poulsen
    1

    Hi Laurence, I'm not completely sure know what your input is - a 2-digit week number you want to split into 2 separate digits, or a string of 2-digit  weeknumbers you want to split into separate weeknumbers, or ...

    Anyway, you could consider using the xslt substring function (http://msdn.microsoft.com/en-us/library/ms256054.aspx)

    >Tommy

     

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 11, 2010 @ 21:51
    bob baty-barr
    1

    Laurence...

    can you use to grab specific indexes of the string?

    <xsl:value-of select="substring(.,1,1)"/>
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 11, 2010 @ 22:03
    Lee Kelleher
    2

    Hi Laurence,

    If you're in a position to use an XSLT extension, try this:

    public static XPathNodeIterator StringToCharArray(string StringToSplit)
    {
        char[] values = StringToSplit.ToCharArray();
        XmlDocument xd = new XmlDocument();
        xd.LoadXml("<chars/>");
        foreach (char value in values)
        {
            XmlNode node = xmlHelper.addTextNode(xd, "char", value.ToString());
            xd.DocumentElement.AppendChild(node);
        }
        return xd.CreateNavigator().Select("/chars");
    }

    Works the same say as umbraco.library.Split(), but turns the string into a Char array.

    Cheers, Lee.

  • Laurence Gillian 600 posts 1219 karma points
    Jun 11, 2010 @ 22:48
    Laurence Gillian
    0

    To confirm, this is an example...

    INPUT = 42

    I want to split this into 4 and 2.

    So I can output

    <span class="digit4">4</span>
    <span class="digit2">2</span>

    Thanks for the responses so far! I will look into this more, but its Friday I'm off out for drinks and good times! :) Hope you are too :)

    Have a lovely weekend, will try some of the above tomorrow :) Laurie

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 12, 2010 @ 00:17
    Chriztian Steinmeier
    4

    Hi Laurence,

    This recursive template will do the trick:

       <xsl:template name="split">
            <xsl:param name="number" />
            <xsl:param name="index" select="1" />
            <xsl:variable name="digit" select="substring($number, 1, 1)" />
            <span class="digit{$digit}">
                <xsl:value-of select="$digit" />
            </span>
            <xsl:if test="$index &lt; string-length($number)">
                <xsl:call-template name="split">
                    <xsl:with-param name="number" select="substring($number, 2)" />
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    
        <!-- Sample call from somewhere else: -->
        <xsl:call-template name="split">
            <xsl:with-param name="number" select="42" />
        </xsl:call-template>
    

    - and a god weekend to you too :-)

    /Chriztian

  • Josh Townson 67 posts 162 karma points
    Jun 12, 2010 @ 11:42
    Josh Townson
    2

    Hi Laurence, you can use the EXSLT string function split (or tokenize) with an empty string, which will separate the string into each character see here: http://www.exslt.org/str/functions/split/str.split.html

    In use it would be:

    <xsl:variable name="value" select="123">
    <xsl:variable name="valueTransform" select="Exslt.ExsltStrings:split($value,'')" />

    which would give the following node set in the $valueTransform variable:

    <token>1</token>
    <token>2</token>
    <token>3</token>

    Not sure if the umbraco.library:Split function works in the same way, but the EXSLT one certainly claims to.

    I do have to say that Chriztian's solution will do exactly what you are asking for,

    /Josh

  • Laurence Gillian 600 posts 1219 karma points
    Jun 14, 2010 @ 12:09
    Laurence Gillian
    0

    Thanks for all the replies on this thread. 

    Really like the solutions suggested with particular note too Chriztian's and Josh's.

    Many thanks! Laurie

Please Sign in or register to post replies

Write your reply to:

Draft