Replacing text within a Variable
I'm trying to highlight certain text when found in the name field by highlighting that text. For instance, We sell magnets and our standard grade is N42. When an order comes in for N52, I want to throw a span tag around N52 and change the background color to yellow.
I have a replace function but the result is the html showing as text. The code below is testing with a <b> tag.
Here is my function:
<xsl:template name="replace-string">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="with" />
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$with" />
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="with" select="$with" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Here is my call:
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="Name" />
<xsl:with-param name="replace" select="'N42'" />
<xsl:with-param name="with" select="'<b>N42</b>'" />
</xsl:call-template>
Here is the result - line 2 shows N42 with the <b> tag around it.
-
Try this.
<xsl:template name="replace-string">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="with" />
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<b><xsl:value-of select="$with" /></b>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="with" select="$with" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Please sign in to leave a comment.
Comments
2 comments