Interpreting newlines with xsl:text?

Greg Mattes picture Greg Mattes · Oct 9, 2008 · Viewed 23k times · Source

I have an XSL stylesheet with content in an xsl:text node like this:

<xsl:text>
foo
bar
baz
</xsl:text>

The stylesheet itself is a text file with "unix-style" newline line terminators. I invoke this stylesheet on Windows as well as unix-like platforms. It would be nice to have the output conform to the conventions of the platform on which it is invoked.

When I run this stylesheet on Windows, the output has carriage return/newline pairs for everything except the contents of the xsl:text node.

Can I instruct the XSLT processor to translate the newline characters in the content of the xsl:text node into platform specific end-of-lines?

More context: I'm invoking the stylesheet from the Apache Ant 1.7.1 XSLT task like this:

<xslt in="in.xml" out="out.xml" style="stylesheet.xsl"/>

The stylesheet header currently looks like this:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xalan="http://xml.apache.org/xslt"
    exclude-result-prefixes="xalan">
    <!-- contents elided -->
</xsl:stylesheet>

Answer

Adam Crume picture Adam Crume · Oct 9, 2008

You could define a parameter for the stylesheet like so:

<xsl:param name="br">
    <xsl:text>&#10;</xsl:text>
</xsl:param>

and pass in the appropriate end of line character(s) by using a nested param element in your Ant script. The default in this example would be a Unix-style newline, of course. I think to output the value, you'd have to use:

<xsl:copy-of select="$br"/>

It's verbose, but it works.