views:

37

answers:

2

Hi,

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

+2  A: 

You can use the xsl:strip element to declare which elements should not have whitespace (or use * for all elements):

<xsl:strip-space elements="*"/>

The counter part is xsl:preserve, which allows you to declare which elements should have whitespace preserved. You could use both:

<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="td span"/>
<!-- strip spaces from all elements apart from td and span elements -->
Oded
When I use the < xsl: strip-space elements =" *"/> it still outputs the CDATA <EOF> with a tab... Any ideas? I have put the strip-space elements = "*" at the top of my file under xsl:output method = "text"
Andez
OK - I've removed the CDATA and replaced with the xsl text and it is fine
Andez
@Oded: The output method is `text`
Alejandro
CDATA causing the problem?
Andez
A: 

The XSLT processor strip white space only text nodes between XSLT instructions from the stylesheet.

So, in this

   <xsl:for-each select="//rows/row"> 
<![CDATA[row id=]]><xsl:value-of select="(@id)"/> 
</xsl:for-each>

xsl:for-each has to text nodes childs: after xsl:value-of there is one white space only text node. This is striped. Before CDATA section there is other white space only text node. This is not striped.

Bottom line: use xsl:text instructions.

Alejandro