Xsl:output method=text text readability
From FVue
Contents
Problem
I want to transform XML to text via Xslt. Because Xslt formats the text (adds indendation, strips whitespace) it seems I can only specify the `text' in a very unreadable way.
For example, I want the following text output, with no indentation:
value1=1 value2=2 value3=3
Try 1. Clean Xslt
This is a clean Xslt I would've liked to see work:
<xsl:template match="root"> value1=<xsl:value-of select="value1"/> value2=<xsl:value-of select="value2"/> value3=<xsl:value-of select="value3"/> </xsl:template>
But the above Xslt doesn't work because: the output lines are indented, the first output line is an empty line, and the last output line doesn't end with a newline.
Try 2. Wrapping everything in xsl:text
<xsl:template match="root"> <xsl:text>value1=</xsl:text><xsl:value-of select="value1"/><xsl:text> </xsl:text> <xsl:text>value2=</xsl:text><xsl:value-of select="value2"/><xsl:text> </xsl:text> <xsl:text>value3=</xsl:text><xsl:value-of select="value3"/><xsl:text> </xsl:text> </xsl:template>
Although the Xslt is nicely indented, it is too verbose and it is difficult to decipher what will be output.
Solution
Avoiding xsl:text where possible
<xsl:template match="root"> <xsl:text/><!-- Prevents output of newline -->value1=</xsl:text><xsl:value-of select="value1"/> value2=<xsl:value-of select="value2"/> value3=<xsl:value-of select="value3"/> <xsl:text><!-- Add newline --> </xsl:text> </xsl:template>
The above Xslt is not nicely indented, but the output text is as readable as I can get with Xslt.
See also
- Converting XMod XML Export to CSV
- Example approach of formatting with `method=text', but the `disable-output-escaping' isn't necessary when xsl:output method=text.
Advertisement