I am extracting data from XML using XSLT 2.0. The data has long lines and I want to fit them into window size by automatically breaking lines.
Is it possible in XSLT?
I am extracting data from XML using XSLT 2.0. The data has long lines and I want to fit them into window size by automatically breaking lines.
Is it possible in XSLT?
You can use the standard XSLT 2.0 function
unparsed-text()
to read a text file directly in your XSLT 2.0 code.Then just use:
Explanation:
This first normalizes the white space, deleting the leading and trailing sequences of whitespace-only characters and replacing any inner such sequence with a single space.
Then the result of the normalization is used as the first argument to the standard XPath 2.0 function
replace()
.The match pattern is any (longest possible sequence of maximum 61 characters that ends with a space.
The replacement argument specifies that any such sequence found should be replaced by the string before the ending space, concatenated with a NL character.
Here is a complete solution, reading and formatting this text from the file
C:\temp\delete\text.txt
:The XSLT code:
The result is a set of lines, each of which doesn't exceed a fixed length of 60:
Update:
In case the text comes from an XML file, this can be done with a minimal change to the above solution:
Here I suppose that all the text is in the only text node child of the top element (named
text
) of the XML document:When this transformation is applied to the XML document above, the same result as with the first solution is produced.
I would imagine that
tokenize()
or<xsl:analyze-string>
could be used to do this efficiently, using a regexp that allows up to (say) 70 characters, and ends with a breaking character (e.g. space).For explicit code, see the XPath and XSLT answers at xquery word wrap.