Scott
Scott

Reputation:

How to change my XSL stylesheet to properly allow carriage returns

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>

Upvotes: 0

Views: 6050

Answers (5)

Tomalak
Tomalak

Reputation: 338316

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.

Upvotes: 0

Andrew Cowenhoven
Andrew Cowenhoven

Reputation: 2816

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>

Upvotes: 1

rasx
rasx

Reputation: 5348

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.

Upvotes: 1

dkackman
dkackman

Reputation: 15559

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.

Upvotes: 0

Chris McCall
Chris McCall

Reputation: 10407

Have you tried the preserve white space tag?

Upvotes: -1

Related Questions