tags:

views:

1086

answers:

1

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?

+1  A: 

Call this template on the string you want to process:

<xsl:template name="break">
  <xsl:param name="text" select="."/>
  <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:

<xsl:template match="msg">
  <xsl:call-template name="break">
    <xsl:with-param name="text" select="text()" />
  </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.

Tomalak
I'm tryin that, doesnt work
Allen
It works for me. Can you post the XML/XSL you try it with?
Tomalak
Tomalak, can you post a sample xml for this?
Bogdan Gavril
I thought I did. What else do you need?
Tomalak