Using XSL 1.0, I found a nice tokenize function. Now I need to for-each over the resulting tokens. I am very new to XSL.
The php equivalent of what I need:
$in = 'a,b,c,d';
$tokens = explode (',', $in);
foreach ($tokens as $token) {
echo $token;
}
Here’s what I have so far. This line will output ‘a,b,c,d’-
<xsl:value-of select="@CommaSeparated" />
This will run that string through the tokenize function-
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select="@CommaSeparated"/>
</xsl:call-template>
And the tokenize function. I understand what this does, just not the format of the data it spits out-
<xsl:template name="tokenize">
<xsl:param name="pText"/>
<xsl:if test="string-length($pText)">
<tag>
<xsl:value-of select=
"substring-before($pText, ',')"/>
</tag>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Firstly, the tokenize function are using is not actually correct. In theory, it should spit out a list of tag elements (strictly speaking a “result tree fragment”) like so
But it is actually missing out the last element
You are probably better off finding another tokenize function here (there must be surely working ones here in StackOverflow).
But, in answer your question about using xsl:for-each over this, you might be tempted to do something like this…
That is to say, store the list of tags in a variable, and then loop over them. However, if you try this in XSLT1.0 you would get an error “Expression must evaluate to a node-set.”. To get around this, you need to use an extension function. EXSLT is probably the most common. You would declare this in your XSLT like so
You could them simply change the xsl:for-each as follows:
So, given the following XML
And the following XSLT
The following is output (with the last tag blank due to the bugged tokenize function you are using)