xslt 将 \n 替换为 <br/>只在一个节点? [英] xslt replace \n with <br/> only in one node?

查看:28
本文介绍了xslt 将 \n 替换为 <br/>只在一个节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个节点 <msg>其中包含一条消息,例如

Hey I have a node <msg> which contains a message such as

string1
字符串 2
sting3

string1
string 2
sting3

但是当它呈现时,它呈现所有一行我如何用 <br/> 替换所有 \n.

but however when it renders, it renders all one line how can i replace all \n with <br />'s.

我试过了

<xsl:value-of select='replace(msg, "&#xA;", "<br/>")'/>

<xsl:value-of select='replace(msg, "&#xA;", "<br/>")' />

但我收到此错误

加载样式表时出错:XSLT/XPath 函数无效.

Error loading stylesheet: Invalid XSLT/XPath function.

我该怎么做?

推荐答案

在要处理的字符串上调用此模板:

Call this template on the string you want to process:

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

像这样(它将在当前节点上工作):

Like this (it will work on the current node):

<xsl:template match="msg">
  <xsl:call-template name="break" />
</xsl:template>

或者像这样,显式传递一个参数:

or like this, explicitly passing a parameter:

<xsl:template match="someElement">
  <xsl:call-template name="break">
    <xsl:with-param name="text" select="msg" />
  </xsl:call-template>
</xsl:template>

我认为您正在使用 XSLT 1.0 处理器,而 replace() 是 XSLT/XPath 2.0 中引入的函数.

I think you are working with an XSLT 1.0 processor, whereas replace() is a function that has been introduced with XSLT/XPath 2.0.

这篇关于xslt 将 \n 替换为 &lt;br/&gt;只在一个节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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