I have an XSLT that is transformed to an XML using System.Xml.Xsl.XslCompiledTransform. The problem is that tags with spaces are converted to empty tags. Below is a minimal sample to reproduce the problem.
Original data:
<data>
<content>A</content>
<content> </content>
<content>B</content>
</data>
Output data:
<?xml version="1.0" encoding="utf-8"?>
<data>
<content>A</content>
<content />
<content>B</content>
</data>
The second tag is wrong! The space must not be eaten like that.
And the XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:preserve-space elements="content"/>
<xsl:template match="//data">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
Is the XSLT wrong? Or should I blame the .NET XSL transformer (I’ve tried both 3.5 SP1 and 4.0)?
Please help!
As David pointed out the problem is that the spaces are stripped out before getting to the XSLT. The solution when using .NET is to use an
XmlReaderto read the input XML – this code should do the trick:where
spaceSample.xmlin the input file,space.xmlis the output andspace.xsltis the transformation.(Tested with .NET 4.0)