I am creating some xsl to transform my xml into text (which will be csv eventually). I am using VS2008. When I use the editor to create the xsl, the transformed output is indented as per my xsl. However, if I edit the xsl and remove the formatted whitespaces it outputs correctly - but doing it like this is a nightmare to work with.
Is there some xsl pre-processor commands or markup I can put in to prevent this? I want to ignore any whitespace in my xsl and only output text using <!CDATA[]]>
or <xsl:text>
.
My XSL is as below - this indents the output
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="text" indent="no"/>
<!-- @* is all class attributes -->
<xsl:template match="/">
<xsl:text>CSV Output</xsl:text>
<!-- Start of output -->
<xsl:for-each select="//rows/row">
<![CDATA[row id=]]><xsl:value-of select="(@id)"/>
</xsl:for-each>
<!-- OK, that is the end of the file -->
<![CDATA[<EOF>]]>
</xsl:template>
</xsl:stylesheet>
The output of this is as follows:
CSV Output
row id=0
row id=1
<EOF>
However, the following outputs correctly:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="text" indent="no"/>
<!-- @* is all class attributes -->
<xsl:template match="/">
<xsl:text>CSV Output</xsl:text>
<!-- Start of output -->
<xsl:for-each select="//rows/row">
<![CDATA[row id=]]><xsl:value-of select="(@id)"/>
</xsl:for-each>
<!-- OK, that is the end of the file -->
<![CDATA[<EOF>]]>
</xsl:template>
</xsl:stylesheet>
This is output correctly as follows:
CSV Output
row id=0
row id=1
<EOF>
I also want to control where a new line is included. In my xsl I am not telling it to include one.
Please help!!
Thanks,
Andez
The XSLT processor strips white-space text nodes in the template only between XSLT elements.
So, in
the
xsl:for-each
element has two white-space text child nodes: One afterxsl:value-of
, which is stripped; the other before the CDATA section, which is not stripped.Bottom line: Use
xsl:text
elements.You can use the
xsl:strip
element to declare which elements should not have whitespace (or use*
for all elements):The counter part is
xsl:preserve
, which allows you to declare which elements should have whitespace preserved. You could use both: