Reputation: 169
My XML:
<root>
<summary>
<article_1>some data
</article_1>
</summary>
<summary>
<article_1>some data
</article_1>
</summary>
.
.
.
</root>
I am trying to copy all summary tags to separate files, but when XSLT transformation is done I am loosing line breaks in the newly created files.
My XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no" version="1.0" encoding="UTF-8" standalone="yes"/>
<xsl:template match="summary">
<xsl:result-document href="{@summary}.xml">
<xsl:copy-of select="." />
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
What am I doing wrong, and why are there no line breaks in my newly created XML anymore?
Upvotes: 2
Views: 5964
Reputation: 17
I have got the variable in XSLT with the line-breaks preserved. But when transformed to HTML using XSL, line-breaks are not preserved. Then understood to use
<pre>
<xsl:value-of select="$VariableName" disable-output-escaping="yes"/>
</pre>
Now able to preserve the line breaks as it comes from the source
Upvotes: 0
Reputation: 786
If your XML input uses CDATA, you can do linefeed-treatment="preserve" in your XSL
Example: XML input
<data>
<Alert>
<![CDATA[PredniOD
Ca32mg Mane
Cholenthly]]>
</Alert>
</data>
XSL 1.0
<fo:block linefeed-treatment="preserve">
<xsl:value-of select="data/Alert" />
</fo:block>
Output
PredniOD
Ca32mg Mane
Cholenthly
Other useful ones
white-space-collapse="false" white-space-treatment="preserve"
Upvotes: 1
Reputation: 22617
You do not lose any line breaks (new lines, that is), even if they appear to have vanished.
Assuming a sensible input XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<summary number="1">
<article_1>text1
</article_1>
</summary>
<summary number="2">
<article_1>text2
</article_1>
</summary>
<!--...-->
</root>
And this stylesheet (this is in XSLT 2.0, as you use result-document()
):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no" version="1.0" encoding="UTF-8" standalone="yes"/>
<xsl:template match="summary">
<xsl:result-document href="{@number}.xml">
<xsl:copy-of select="." />
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
This results in several output documents, like the one named 1.xml
shown here:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><summary number="1">
<article_1>text1
</article_1>
</summary>
As you can see, there is still a new line. It is no longer encoded as an entity reference (

), but translated to an actual break. If you are looking at the result files with a browser like Firefox, it might trick you into thinking that the newlines are no longer there, but that's only formatting automatically applied by the brower's parser.
Note that your post has severe issues besides the newline problem.
@summary
, yet in your input XML there is no such attribute. In the input I have shown there is a number
attribute which is translated into the file names.result-document()
function that is available only in XSLT 2.0 (i.e. with an XSLT 2.0 processor). Why then is your stylesheet in version="1.0"
?Upvotes: 0
Reputation: 8227
The newline character is treated as white space, whether you encode it or not. You can add the directive:
<xsl:preserve-space elements="article_1"/>
to make sure that the white space is preserved.
Upvotes: 2