XSL如何从文本中移除换行符

3
我想移除所有文本后面的换行符,这些文本都是以“查看展品”开头的。
在notepadd++中显示不需要的换行符: alt text http://img13.imageshack.us/img13/9803/clcflinebreak.png 目前我已经做到了:
<xsl:template match="p">
<!-- output everything but the See the exhibit text should have the line break removed -->

</xsl:template>

有什么想法吗?谢谢!
2个回答

14
如果您正在使用转换生成HTML输出,最简单的方法通常是:

<xsl:value-of select="normalize-space($text)"/>

normalize-space 函数会去掉字符串开头和结尾的空格,并将字符串中多个空格字符替换为一个空格。

要去掉字符串末尾的一个 CR/LF 对:

<xsl:choose>
   <xsl:when test="substring(., string-length(.)-1, 2) = '&#xD;&#xA;'">
      <xsl:value-of select="substring(., 1, string-length(.)-2)"/>
   </xsl:when>
   <xsl:otherwise>
      <xsl:value-of select="."/>
   </xsl:otherwise>
</xsl:choose>

4
“+1 for normalize-space” - 刚刚为我省掉了很多麻烦。 (注:原文中的“+1”是一种在网上用于表示赞同或支持的表达方式,意思是“加一分”或“给一个赞”。normalize-space是一种在编程中用于清除字符串中多余空格的函数。) - Chris B

1
        <!-- Get text. Replace all “break with “ -->
        <xsl:variable name="linebreak">
            <xsl:text>
</xsl:text>
        </xsl:variable>
        <xsl:variable name="text">
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="."/>
                <xsl:with-param name="replace" select="concat('“',$linebreak)" />
                <xsl:with-param name="with" select="string('“')"/>
            </xsl:call-template>
        </xsl:variable>


        <xsl:value-of select="$text"/>

9
“replace-string”模板应该在哪里? - Robert Rossney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接