xslt replace \n with
only in one node?

2019-04-25 11:41发布

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

string1
string 2
sting3

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

i've tried

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

but i get this error

Error loading stylesheet: Invalid XSLT/XPath function.

how do i do this?

标签: xslt
3条回答
萌系小妹纸
2楼-- · 2019-04-25 12:17

<pre>
Jayakumar Kulkarni (Consultant) :
Rematr 01

Jayakumar Kulkarni (Consultant) :
3rd comment
</pre>

Try this above tag

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-25 12:20

You can also achieve this by simple HTML tag,
Try this <pre> tag before your msg and close it after msg.

Jayakumar Kulkarni (Consultant) :
Remark

Jayakumar Kulkarni :
Rematr 01

Jayakumar Kulkarni :
comment
查看更多
成全新的幸福
4楼-- · 2019-04-25 12:25

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>

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.

查看更多
登录 后发表回答