XSL output method text including whitespaces in xsl

前端 未结 2 977
悲哀的现实
悲哀的现实 2021-01-14 06:00

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

相关标签:
2条回答
  • 2021-01-14 06:09

    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>
    
    0 讨论(0)
  • 2021-01-14 06:16

    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 讨论(0)
提交回复
热议问题