I have a an attribute who's value may be one or more text strings all delimited by a comma. I wish to transform using XSL the attribute value(s) into their own element;
e.g
<post title='Hello World" tags="Test,Hello,World />
In which I would like it transformed to;
<post>
<title>Hello World</title>
<tag>Test</tag>
<tag>Hello</tag>
<tag>World</tag>
</post>
Is this possible? TIA
I really liked Dimitre answer, but had a small bug with his XSLT 1.0 Solution. When I had "Value,Value,Value" , it would only split the first two and skip the last.
Edit This was not a bug, I didn't notice he was calling the template with CONCAT
Here is my modification to his tokenize xslt 1 template.
Usage Example
Outputs
The first thing you should do is find the person who used an attribute where he should have used elements and make him stop. The reason we accept the verbosity of XML is that it gives us the benefit of not having to figure out how to parse data. If you're going to pack your XML with data that has to be parsed, why are you using XML in the first place?
There are several ways to do this.
I. Using a recursively-called named template in XSLT 1.0 This transformation:
when applied on the originally-provided XML document (corrected to be well-formed):
produces the required result:
II. Using the
str-split-to-words
template/function from FXSL 1.xHere FXSL provides the tokenization functionality:
When applied on the same XML document as before, the same correct output is produced.
III. Using the XPath 2.0 standard function
tokenize()
from an XSLT 2.0 transformationThis is the easiest way -- if one can use an XSLT 2.0 processor.
The following XSLT 2.0 transformation:
when applied on the same XML document again produces the wanted result.