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!
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 ...
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...
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
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.
No, sadly not its a two digit week number. /L (so ranges from 0 - 52)
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
Laurence...
can you use to grab specific indexes of the string?
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.
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
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 < 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
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:
which would give the following node set in the $valueTransform variable:
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
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
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.