0

I'm trying to replace line breaks (&#xA;) with <br />. However, I cannot get the actual <br /> tags to show up.

Invocation:

<xsl:variable name="br"><br /></xsl:variable>
<xsl:call-template name="string-replace-all">
    <xsl:with-param name="text" select="@SolutionNote" />
    <xsl:with-param name="replace" select="'&#xA;'" />
    <xsl:with-param name="by" select="$br" />
</xsl:call-template>

string-replace-all is taken from the answer given in this question (pasted here for quick reference):

<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="$text = '' or $replace = ''or not($replace)" >
            <!-- Prevent this routine from hanging -->
            <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Testing with a string literal instead of $br confirms that there are indeed things that are being replaced. The line breaks themselves are also gone--just replaced with seemingly nothing. What is the correct way to declare/use the br variable?

Community
  • 1
  • 1
idlackage
  • 2,715
  • 8
  • 31
  • 52

1 Answers1

0

The problem is with this line:

<xsl:value-of select="$by" />

This is because xsl:value-of outputs the text value of whatever is being selected. In this case, if this is XSLT 1.0, the $by variable is actually a result-tree-fragment.

What you need to do, is use xsl:copy-of instead:

<xsl:copy-of select="$by" />
Tim C
  • 70,053
  • 14
  • 74
  • 93