如何用 xslt 转换中的新行替换给定字符?

How replace a given character by a new line in a xslt transformation?

我目前正在处理 XSLT 文件以将 XML 文件转换为 HTML 文件。

在我的 XML 中,我将数据检索为

    <ns0:Key>
        <ns0:Field>Comments</ns0:Field>
        <ns0:Value>line 1 ¤ line 2 ¤ etcaetera</ns0:Value>
    </ns0:Key>

我想在转换后的 HTML 页面中用新行替换“¤”。

我试试这个模板

<xsl:template name="string-replace">
            <xsl:param name="string" />
            <xsl:param name="replace" />

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

我是这样应用的:

<xsl:variable name="string-mod">
            <xsl:call-template name="string-replace">
                    <xsl:with-param name="string" select="./*[local-name()='Key'][*[local-name()='Field']='Comments']/*[local-name()='Value']" />                                
                    <xsl:with-param name="replace" select="'&#164;'" />        
            </xsl:call-template>
    </xsl:variable>

我用不同的字符和标记进行了一些尝试(我让一个在模板中发表评论),但目前没有任何效果。

如果有人有任何想法,那就太好了:)

谢谢

请注意,我必须使用 XSLT 1。

HTML 文档中的换行符不会导致文本在浏览器中换行显示。浏览器将所有白色space 字符序列(包括换行符)折叠成一个space。要在新行上显示文本,您需要使用 <br> 标签或类似标签。

所以在您的文本中插入换行符没有任何效果。您需要插入标记。

[已解决]

谢谢,这周末我想出了一个解决方案。

使用此模板:

<xsl:template name="string-replace">
            <xsl:param name="string" />
            <xsl:param name="replace" />
            <xsl:param name="with" />
            <xsl:choose>
                <xsl:when test="contains($string, $replace)">
                    <xsl:value-of select="substring-before($string, $replace)" />
                    <br/>
                    <xsl:call-template name="string-replace">
                        <xsl:with-param name="string" select="substring-after($string,$replace)" />
                        <xsl:with-param name="replace" select="$replace" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$string" />
                </xsl:otherwise>
           </xsl:choose>
        </xsl:template>

这样调用:

 <xsl:call-template name="string-replace">
        <xsl:with-param name="string" select="./*[local-name()='Key'][*[local-name()='Field']='Comments']/*[local-name()='Value']" />
        <xsl:with-param name="replace" select="'&#164;'" />
    </xsl:call-template>