【发布时间】:2010-09-10 08:56:51
【问题描述】:
我有 XML 中的数据,其中包含我想在输出 HTML 中保留的换行符、空格和制表符(所以我不能使用
)但我也希望这些行在已到达屏幕(所以我无法使用
)。
【问题讨论】:
标签: xslt
我有 XML 中的数据,其中包含我想在输出 HTML 中保留的换行符、空格和制表符(所以我不能使用
)但我也希望这些行在已到达屏幕(所以我无法使用
)。
【问题讨论】:
标签: xslt
我和一位同事 (Patricia Eromosele) 提出了以下解决方案:(有更好的解决方案吗?)
-template>
来源:http://jamesjava.blogspot.com/2008/06/xsl-preserving-line-feeds-tabs-and.html
【讨论】:
真的,我会选择一个正确支持此功能的编辑器,而不是通过更多的 XML 来解决它。
【讨论】:
不确定这是否相关,但是否没有preservespace 属性和xml 之类的?
【讨论】:
另一种表达方式是,您希望将所有空格对转换为两个不间断空格,将制表符转换为四个不间断空格,并将所有换行符转换为 <br> 元素。在 XSLT 1.0 中,我会这样做:
<xsl:template name="replace-spaces">
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="contains($text, ' ')">
<xsl:call-template name="replace-spaces">
<xsl:with-param name="text" select="substring-before($text, ' ')"/>
</xsl:call-template>
<xsl:text>  </xsl:text>
<xsl:call-template name="replace-spaces">
<xsl:with-param name="text" select="substring-before($text, ' ')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($text, '	')">
<xsl:call-template name="replace-spaces">
<xsl:with-param name="text" select="substring-before($text, '	')"/>
</xsl:call-template>
<xsl:text>    </xsl:text>
<xsl:call-template name="replace-spaces">
<xsl:with-param name="text" select="substring-before($text, '	')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($text, '
')">
<xsl:call-template name="replace-spaces">
<xsl:with-param name="text" select="substring-before($text, '
')" />
</xsl:call-template>
<br />
<xsl:call-template name="replace-spaces">
<xsl:with-param name="text" select="substring-after($text, '
')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
不能使用尾递归有点麻烦,但除非文本非常长,否则应该不是真正的问题。
XSLT 2.0 解决方案将使用 <xsl:analyze-string>。
【讨论】: