Page 1 of 1

XSLT And Newline Character Handling

Posted: Fri Feb 21, 2014 9:27 pm
by Jamil
It is well documented that I cannot create portable XSLT, since different operating systems handle new line characters differently. The XML standard states that an XML parser is required to store only a linefeed character at the end of a line (&#xa;). To ensure that my XSLT is portable, I used <xsl:text>&#xa;</xsl:text> for new lines as opposed to <xsl:text>&#xd;&#xa;</xsl:text>. This all makes sense to me. When I use oxygen to test out my XSLT, the resulting output shows only linefeed characters for line ends.

I am writing Java code to perform transformations and using XSLT 1.0. I thought that I would have to perform replacements of the &#xa; to &#xd;&#xa;. To my surprise, I did not have to do this. The file stream output already had the proper line end characters that Windows expects.

My question is why am I seeing this difference in line end characters for the same XSLT I generated and tested using oxygen?

Thanks.

Re: XSLT And Newline Character Handling

Posted: Tue Feb 25, 2014 6:25 pm
by adrian
Hello Jamil,

It depends on what XSLT processor you're using in your Java code and with what flags.
XSLT processors may format the output with a specific line terminator or the default one of the used platform.

Regards,
Adrian

Re: XSLT And Newline Character Handling

Posted: Wed Feb 26, 2014 3:16 pm
by Jamil
Hi Adrian. Thanks for your response.

I am using Java 1.7's XSLT processor, and I am not setting any specific options that is adding operating specific line end characters. It very well could be that Java is doing this all on its own to save me time.

I am using a DOMSource as my XML dource document, and a ByteArrayOutputStream for my output. My syntax to do the XSLT transformation is simply:

Code: Select all


ByteArrayOutputStream outputTarget = null;
DOMSource domSource = createDomSource();
transformer.transform(domSource, outputTarget);
FileOutputStream output = new FileOutputStream (new File("output.txt"));
outputTarget.writeTo(output);
output.close();
This is all I have. Something is automatically adding carriage return and line feed characters to line ends.