H
Hvid Hat
Hi
I want to highlight (make it bold) a word in some text I'm getting in XML
format. My plan was to replace the word with a bold (or span) tag with the
word within the tag. I've found the code below and it works fine as long
as I'm not adding tags around the to parameter. Can anyone explain to me
why it doesn't work with tags? And it needs to be XSLT 1.0.
This works: X<xsl:value-of select="'little steak'"/>X
This doesn't work: <b><xsl:value-of select="'little steak'"/></b>
And the code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- reusable replace-string function -->
<xsl:template name="replace-string">
<xslaram name="text"/>
<xslaram name="from"/>
<xslaram name="to"/>
<xsl:choose>
<xsl:when test="contains($text, $from)">
<xsl:variable name="before" select="substring-before($text, $from)"/>
<xsl:variable name="after" select="substring-after($text, $from)"/>
<xsl:variable name="prefix" select="concat($before, $to)"/>
<xsl:value-of select="$before"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsltherwise>
<xsl:value-of select="$text"/>
</xsltherwise>
</xsl:choose>
</xsl:template>
<!-- test the function -->
<xsl:template match="/">
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="'Mary had a little lamb, little lamb, little lamb.'"/>
<xsl:with-param name="from" select="'little lamb'"/>
<xsl:with-param name="to">
<!-- X<xsl:value-of select="'little steak'"/>X -->
<b><xsl:value-of select="'little steak'"/></b>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet
I want to highlight (make it bold) a word in some text I'm getting in XML
format. My plan was to replace the word with a bold (or span) tag with the
word within the tag. I've found the code below and it works fine as long
as I'm not adding tags around the to parameter. Can anyone explain to me
why it doesn't work with tags? And it needs to be XSLT 1.0.
This works: X<xsl:value-of select="'little steak'"/>X
This doesn't work: <b><xsl:value-of select="'little steak'"/></b>
And the code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- reusable replace-string function -->
<xsl:template name="replace-string">
<xslaram name="text"/>
<xslaram name="from"/>
<xslaram name="to"/>
<xsl:choose>
<xsl:when test="contains($text, $from)">
<xsl:variable name="before" select="substring-before($text, $from)"/>
<xsl:variable name="after" select="substring-after($text, $from)"/>
<xsl:variable name="prefix" select="concat($before, $to)"/>
<xsl:value-of select="$before"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsltherwise>
<xsl:value-of select="$text"/>
</xsltherwise>
</xsl:choose>
</xsl:template>
<!-- test the function -->
<xsl:template match="/">
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="'Mary had a little lamb, little lamb, little lamb.'"/>
<xsl:with-param name="from" select="'little lamb'"/>
<xsl:with-param name="to">
<!-- X<xsl:value-of select="'little steak'"/>X -->
<b><xsl:value-of select="'little steak'"/></b>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet