I am extracting data from XML using XSLT. I want to see data in xml-editor.How to wrap text to fit window in XSLT?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match='/'>
<wawes>
<xsl:for-each select="//VARIABLE">
<xsl:sort select="@ID" order="descending"/>
<wave>
<id>
<xsl:value-of select="@ID" ></xsl:value-of>
</id>
<NAME>
<xsl:value-of select="NAME"/>
</NAME>
</wave>
</xsl:for-each>
</wawes>
</xsl:template>
</xsl:stylesheet>
I have had reasonable success using Regular Expressions to break up a string using
XSLT-2
'sreplace
function (replacing with$1\n
). I know you are concerned withXSLT-1
, but from what I have found, regular expressions are the only way to do this without iterative parsing.Initially, I found this question and answer, but I couldn't get it to work within the Regular Expressions subset allowed with XSLTs, mainly hobbled by the lack of positive lookahead.
Although XSLT is about XML manipulation, my line of work has proven that that is not the only use case as we use XSLT to perform mass data transformations for system and client interoperability, not only on XML data, but plaintext and formatted text (i.e. CSV) data as well.
One such transformation required the implosion of a set of address elements:
into a text block that would resemble:
however, the field that this information would be put into was limited to only 4 lines, and each line being 50 characters. To conserve space, I put the
PostCode
,County
, andCountryCode
on a single line and appended it separately, so I had 3 lines of 50 characters instead. I then took the remaining fields, and CSV'd them together (unless one was blank), such that it looked like:Looking at the question and answer I linked to above, I got the regular expression (swapped 16 for 50 in the lengths):
however, it would not work in XSLT.
For starters, it didn't recognise
(?>
(atomic groups), or(?=
(positive lookaheads, but after swapping them with anonymous groups, it worked.But after that, I got errors about it matching a zero-length string. That was being caused by the last occurrences of
|$
, which was causing it to match to the end of line, but without that, it finally worked. It was not as neat as I had hoped, but it did compress the single line of text into a set of lines that were narrower than 50 characters at each point. The final regular expression was:Now, in my case which doesn't apply to you, I had a line restriction too, but I just discarded the remaining lines.
Hope that's of some help to you
Wrapping text in order to fit the screen size or resolution is usually not related to XSLT. XSLT is only concerned with transforming XML, not with the presentation and layout of XML data.
And if you think about it that's not too bad: the same XML must be equally accessible to all types of screen sizes, resolutions - and all types of applications, including XML editors. It would be presumptuous to fit XML to your unique combination of these three factors (what about window size?).
Instead, as pointed out by @rene, wrapping text comes as a feature with virtually every text editor. For example, there is CTRL + W for this in UltraEdit.
N.b. it is not technically impossible to enforce wrapping text in XSLT. You can introduce newline characters in the text content of your elements. But
My other answer provides a solution using
XSLT-2
, whereas the initial scope of this question wasXSLT-1
.This premise is based on the idea of repeatedly reiterating over the target string, slicing off variably sized, sub 50 segments and placing each one on its own line, and continuing this function until there is no more text available.
Works perfectly in many tests. Please forgive its craziness, I threw it together in MapForce