I have a string in which data is separated by a delimiter like "|" and is present in a variable.
I would like to create an array in the XSL by dividing the above string based on the delimiter and would like to access the same in the in a for loop.
Please help me in this regard. Please also let me know if anyone need any more information.
String is "Test1|Test2|Test3|Test4" and would like to get a variable TEMP which would be an array of data from the string and would like to access as TEMP[index].
I have tried to use the tokenize function after the inputs from the forum members to get the values from the string but was not successful. I am not getting the string values in the loop.
<xsl:variable name="temp" xmlns:str="http://exslt.org/strings" select="str:tokenize(normalize-space(' Test1$,$Test2$,$Test3$,$Test4 '),'$,$')"/>
<xsl:for-each xmlns:str="http://exslt.org/strings" select="str:split(normalize-space(' 1$,$2$,$3$,$4$,$5$,$6 '),'$,$')">
    <xsl:variable name="index" select="position()"/>
    <xsl:value-of select="$temp[$index]"/>
</xsl:for-each>
Regards, Lakshman
String is
"Test1|Test2|Test3|Test4"and would like to get a variableTEMPwhich would be an array of data from the string and would like to access asTEMP[index].
+1 for a good question.
This XSLT 1.0 transformation:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:variable name="vrtfTokens">
  <xsl:apply-templates/>
 </xsl:variable>
 <xsl:variable name="vTokens" select=
   "ext:node-set($vrtfTokens)/*"/>
 <xsl:template match="/">
  <xsl:for-each select=
   "document('')//node()[not(position() > count($vTokens))]
   ">
   <xsl:variable name="vPos" select="position()"/>
    <xsl:copy-of select="$vTokens[$vPos+0]"/>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="text()" name="split">
  <xsl:param name="pText" select="."/>
  <xsl:if test="string-length($pText)">
   <xsl:variable name="vToken" select=
    "substring-before(concat($pText,'|'), '|')"/>
   <s><xsl:value-of select="$vToken"/></s>
   <xsl:call-template name="split">
    <xsl:with-param name="pText" select=
    "substring-after($pText, '|')"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t>Test1|Test2|Test3|Test4</t>
creates a variable vTokens that contains elements named s, each of which has as its only text child a token from the '|'-delimited string "Test1|Test2|Test3|Test4".
Then the transformation outputs each of these s elements using an "index".
The wanted, correct result is produced:
<s>Test1</s>
<s>Test2</s>
<s>Test3</s>
<s>Test4</s>
In case we want just the tokens (strings) themselves, we'd use:
string($vTokens[someIndex])
                        It won't be an array but a sequence, and you must have a XSLT 2.0 processor. You can use the tokenize() function :
<xsl:variable name="temp" as="xs:string*" select="tokenize('Test1|Test2|Test3|Test4','\|')"/>
You can also pass a string variable as first argument of tokenize.
Then you use :
<xsl:value-of select="$temp[$index]"/>
EDIT : achieve this in xslt 1.0 is not possible unless you use some extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With