How to modify XSL stylesheet to properly allow carriage return

Hey I was wondering if anyone knows how to modify the following XSL stylesheet so that ANY text in my transformed XML will preserve carriage returns and line feeds (which will be \ r \ n when I cast it to XML). I know which I should be using in some way, but I can't figure out how to get it to work.

<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>

0


source to share


5 answers


In the above code, you cannot apply templates and expect this template to be called:

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

      

If you have a node in your XML named "\ r \ n" that is illegal anyway. I think you want to make this call explicitly when you want to return the carriage:



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

      

Here's an example of a template that can be 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>

      

+1


source


Answers Chris and dkackman are on the mark, but we also need from time to time to listen to the W3C :

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



This means that in your XSLT, you can experiment with some combination of &#xA;

and &#xD;

. Remember, different operating systems have different line completion strategies .

+1


source


Have you tried keeping the space tag ?

0


source


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

Any whitespace you absolutely want to display in the output stream, I would wrap in <xsl:text></xsl:text>

I also highly recommend specifying <xsl:output/>

to control the formatting of the output.

0


source


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, your application needs some work.

On non-empty text nodes, XML will preserve line breaks by definition. They are also stored within attribute nodes, unless the product you are using is not spec.

But outside of text nodes (or in those empty text nodes between elements), line breaks are considered misplaced spaces, and you shouldn't rely on them or waste time creating or saving them.

There is <xsl:output indent="yes" />

one that makes some (processor-specific XSLT) pretty-printable, but your application shouldn't rely on such things.

0


source







All Articles