I have some data in an XML element that looks like this:
<item value='category1,category2'>Item Name</item>
The bit I’m interested in is the value attribute. I’m able to get the data contained in this attribute into a template which looks like this:
<xsl:template name='RenderValues'> <xsl:param name='ValueList' /> <xsl:value-of select='$ValueList' /> <!-- outputs category1,category2--> </xsl:template>
What I want to do is to process the comma separated values in an efficient manner. What is the best way to render something like the following from inside the RenderValues template?
<a href='x.asp?item=category1'>category1</a> <a href='x.asp?item=category2'>category2</a>
In XSLT 2.0/XPath 2.0 use the standard XPath 2.0 function tokenize().
In XSLT 1.0 one needs either to write a recursively called template or, more conveniently, use the
str-split-to-wordsfunction/template of the FXSL library.Here is an example of the latter:
When the above transformation is applied on the provided XML document:
the wanted result is produced:
The
pDelimitersparameter of this template allow multiple delimiting characters to be specified. In the above example, any separating character can be either a comma, a space or a new line character.