I have a text file which looks like that:
XXX^YYYY^AAAAA^XXXXXX^AAAAAA....
Fields are separated using a caret(^), my presumptions are:
the first field = NAME
the second filed = Last name
third field = Address
etc..
I would like to turn it into a valid XML using xsl (XSLT). such as:
<name>XXX</name>
<l_name>YYYY</l_name>
I know It can be done easily with Perl, but I need to do it with XSLT, if possible.
Text (non-XML) files can be read with the standard XSLT 2.0 function
unparsed-text()
.Then one can use the standard XPath 2.0 function
tokenize()
and two other standard XPath 2.0 functions that accept regular a expression as one of their arguments --matches()
andreplace()
.XSLT 2.0 has its own powerful instructions to handle text processing using regular expressions:: the
<xsl:analyze-string>
, the<xsl:matching-substring>
and the<xsl:non-matching-substring>
instruction.See some of the more powerful capabilities of XSLT text processing with these functions and instructions in this real-world example: an XSLT solution to the WideFinder problem.
Finally, here is an XSLT 1.0 solution:
When this transformation is applied to the following XML document:
the wanted, correct result is produced:
Tokenizing and sorting with XSLT 1.0
If you use xslt 2.0 it's much simpler: fn:tokenize(string,pattern)