tags:

views:

785

answers:

5

Hey, I was wondering if anybody knew how to alter the following XSL stylesheet so that ANY text in my transformed XML will retain the carriage returns and line feeds (which will be \r\n as I feed it to the XML). I know I'm supposed to be using in some way but I can't seem to figure out how to get it working

<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"> <xsl:template match=\"/\"><xsl:apply-templates /></xsl:template><xsl:template match=\"\r\n\"><xsl:text>&#10;</xsl:text></xsl:template><xsl:template match=\"*\"> <xsl:element name=\"{local-name()}\"><xsl:value-of select=\"text()\"/><xsl:apply-templates select=\"*\"/></xsl:element ></xsl:template></xsl:stylesheet>

A: 

Have you tried the preserve white space tag?

Chris McCall
A: 

It's not completely clear what you are trying to accomplish but...

Any whitespace that you absolutely want to show up in the output stream I would wrap in <xsl:text></xsl:text>

I would also highly recommend specifying an <xsl:output/> to control the output formatting.

dkackman
+1  A: 

The answers from Chris and dkackman are on the mark but we also need to listen to the W3C every now and again:

XML parsed entities are often stored in computer files which, for editing convenience, are organized into lines. These lines are typically separated by some combination of the characters carriage-return (#xD) and line-feed (#xA).

This means that in your XSLT you can experiment with some combination of &#xA; and &#xD;. Remember that different operating systems have different line-ending strategies.

rasx
+1  A: 

In your code above you can't apply templates and expect this template to get called:

<xsl:template match="\r\n\"> 
    <xsl:text>&#10;</xsl:text>
</xsl:template>

Unless you have a node in your XML named "\r\n" which is an illegal name anyhow. I think what you want to do is make this call explicitly when you want a carriage return:

<xsl:call-template name="crlf"/>

Here is an example of the template that could get called:

<xsl:template name="crlf">
    <xsl:text>&#x0D;</xsl:text>
    <xsl:text>&#x0A;</xsl:text>
    <!--consult your system doc for appropriate carriage return coding -->
</xsl:template>
Andrew Cowenhoven
Thanks alot for the answers....everyoneYou're right about the fact that I probably shouldn't be using XML/XSL in the way that I am...but it also isn't so much my choice as it's a pre-built (but very imcomplete) system that I was tasked to "finish off" without having to redesign anything. I think the above template might work for me if I apply it at the right time.
A: 

Your question sounds like you want to control the format of the output XML. My advice: just don't.

XML is data, not text. The format it is in should be completely irrelevant to your application. If it is not, then your application needs some reworking.

Within non-empty text nodes, XML will retain line breaks by definition. Within attribute nodes they are retained as well, unless the product you use does not adhere to the spec.

But outside of text nodes (or in those empty text nodes between elements) line breaks are considered irrelevant white space and you should not rely on them or waste your time trying to create or retain them.

There is <xsl:output indent="yes" />, which does some (XSLT processor-specific) pretty-printing, but your application should not rely on such things.

Tomalak