开发者

XSL output method text including whitespaces in xsl

开发者 https://www.devze.com 2023-01-18 14:42 出处:网络
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,

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

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

the xsl:for-each element has two white-space text child nodes: One after xsl:value-of, which is stripped; the other before the CDATA section, which is not stripped.

Bottom line: Use xsl:text elements.

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


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 -->
0

精彩评论

暂无评论...
验证码 换一张
取 消