I've tried a variety of solutions but can't seem to get this working. I have searched stackoverflow and found many hints, many of which I have tried, to no avail.
I am doing an XSL transformation of XML to XML (FOP) for PDF creation. The source XML has <code> elements whose contents start with a CDATA declaration. The transformation removes newlines.
Example input XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<myxml>
<code><![CDATA[import java.nio.charset.Charset;
import com.my.library.AClass;
import com.my.library.AnotherClass;
public String getStringValue(String key) {
// Just some ramblings
// Dummy code...
if (key != null && key.length() > 0) {
System.out.println(key);
}
}
]]></code>
</myxml>
Example XSL:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://ift.tt/tCZ8VR"
xmlns="http://ift.tt/VnWImq"
xmlns:fo="http://ift.tt/vyPH5E"
exclude-result-prefixes="fo"
>
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" cdata-section-elements="code"/>
<xsl:preserve-space elements="code" />
<fo:block font-family="Courier New" font-size="12pt" color="black"
space-after="12pt" space-before="12pt" space-before.precedence="4">
<fo:block>
<xsl:text>Copy</xsl:text>
<xsl:copy>
<xsl:value-of select="code"/>
</xsl:copy>
</fo:block>
<fo:block>
<xsl:text>Copy Text</xsl:text>
<xsl:copy>
<xsl:value-of select="code/text()"/>
</xsl:copy>
</fo:block>
<fo:block>
<xsl:text>Original</xsl:text>
<xsl:value-of select="code"/>
</fo:block>
<fo:block>
<xsl:text>Normalise space</xsl:text>
<value-of select="normalize-space(code)" disable-output-escaping="yes"/>
</fo:block>
<fo:block>
<xsl:text>Copy with extra CDATA wrapper?</xsl:text>
<xsl:copy>
<xsl:text disable-output-escaping="yes"><![CDATA[<![CDATA[]]></xsl:text>
<xsl:value-of select="code"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:copy>
</fo:block>
<fo:block>
<xsl:text>Again, an attempt to wrap</xsl:text>
<xsl:text disable-output-escaping="yes">
<![CDATA[
</xsl:text>
<xsl:value-of select="code" />
<xsl:text disable-output-escaping="yes">
]]>
</xsl:text>
</fo:block>
</fo:block>
Permutations... I have tried all permutations of the following:
Global Declarations: With Both cdata-section and preserve-space, With only one or the other
Source xml document containing/not containing xml:space="preserve", on both code element and outer element.
In total, 6 permutations.
These settings seem to make little difference for me. The following is output for each test block:
- Block 'Copy': Empty (no text output)
- Block 'Copy Text': Empty (no text output)
- Block 'Original': The code, but all newlines removed
- Block 'Normalise space': Empty (no text output)
- Block 'Copy with extra CDATA wrapper?': Empty (no text output)
- Block 'Again, an attempt to wrap': The code, but all newlines removed
Any help will save my sanity.
Yours in eternal gratitude.
Luke (Slowly going out of my mind).
No comments:
Post a Comment