I need to turn the values in a xml attribute through xsl into a svg rect pos x and y
Therefore need to parse from
Examples
<item value="20 10 40" />
<item value="200 0 100" />
<item value="2666 10 40 95" />
parse each item value attribute following some rules (that's the part I'm not sure about, how to extract the numbers into separate variables)
like
<item value="20 10 40" />
X Z Y
Extract X (20) in var1 and Y (40) in var2
into
<rect x="{$var1}" y="{$var2}" />
(if I want the first and third value in this case)
Basically I need to understand how to parse any TWO of a series of multiple values contained within the attribute VALUE and pass them into the rect vars as var1 and var2 separately.
From my research so far, I have found out 2 methods but not sure how to apply in this case, either substring-before and after or tokenize, please note this needs to work loaded directly in a browser.
Edit1
I have found an ugly solution so far to extract the data for a case of 3 numbers
Xml
<item value="20 10 40" />
Xsl
Value 1 <xsl:value-of select="substring-before (@value, ' ')"/>
Value 2 <xsl:value-of select="substring-before(substring-after (@value, ' '), ' ')"/>
Value 3 <xsl:value-of select="substring-after(substring-after (@value, ' '), ' ')"/>
Result
Value 1 20
Value 2 10
Value 3 40
So looking for something cleaner, perhaps recursive that accept any amount of numbers in the string and parse all.
Extracting values from a delimited list in XSLT 1.0 is awkward, since it has no
tokenize()
function.If the number of values in the list is small (like in your example), you can use nested
substring-before()
andsubstring-after()
calls, as shown (now) in your question.A more generic solution, which is also more suitable to handle larger lists, would use a recursive named template, for example:
Example of call:
Demo:http://xsltransform.net/ncdD7mh
Here is an example using the EXSLT extensions, online at http://xsltransform.net/bnnZW2, the code is
the input is
the output is
It should be clear how to access other tokens by the position, of course you can also
for-each
orapply-templates
over$tokens
.Note that XSLT processors in browsers might not allow you to import or include a stylesheet module from a different domain, so you would to make sure you put the referenced stylesheet module (
str.tokenize.template.xsl
) on your own server and reference it from there.