XSLT And Newline Character Handling

Here should go questions about transforming XML with XSLT and FOP.
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

XSLT And Newline Character Handling

Post 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.
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XSLT And Newline Character Handling

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: XSLT And Newline Character Handling

Post 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.
Post Reply