XSLT - 用单个新行替换多个新行 [英] XSLT - Replace multiple new line with single new line

查看:32
本文介绍了XSLT - 用单个新行替换多个新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 XSLT 的结果中用一个换行符替换多个连续的换行符.

I'm looking to replace multiple consecutive new line characters with a single new line character in the result of an XSLT.

在一个 xml 节点中,我可以有以下内容:

Within an xml node I could have the following:

<description>
    A description that goes over multiple lines, because:

        - someone inputted the data in another system
        - and there are gaps between lines

    Another gap above this
</description>

我希望此节点的文本显示如下:

I'd like the text out of this node to appear as so:

A description that goes over multiple lines, because:
    - someone inputted the data in another system
    - and there are gaps between lines   
Another gap above this

有没有办法用 XSLT 做到这一点?使用 XSLT 1.0 (libxslt)

Is there a way to do this with an XSLT? Using XSLT 1.0 (libxslt)

推荐答案

怎么样:

<xsl:template match="description">
    <xsl:call-template name="normalize-returns">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="normalize-returns">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&#10;&#10;')">
            <!-- recursive call -->
            <xsl:call-template name="normalize-returns">
                <xsl:with-param name="text">
                    <xsl:value-of select="substring-before($text, '&#10;&#10;')"/>
                    <xsl:text>&#10;</xsl:text>
                    <xsl:value-of select="substring-after($text, '&#10;&#10;')"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

这篇关于XSLT - 用单个新行替换多个新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆