XSLT: XML: Replace Function/Functionality
I would like to product the following html
words<block>words</block>
From this node i开发者_StackOverflow社区n an xml doc
<text>words bli!wordsbli!</text>
the bli! represents a tag that will occur in random spots with in the node of the xml doc. Is it possible to replace the bli! with using an xslt function?
There is nothing built into XSLT 1.0 that will do basic string replacement.
This post goes over a good method for implementing this functionality.
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<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>
Here's how it is called:
<xsl:variable name="myVar">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="'This is a sample text : {ReplaceMe} and {ReplaceMe}'" />
<xsl:with-param name="replace" select="'{ReplaceMe}'" />
<xsl:with-param name="by" select="'String.Replace() in XSLT'" />
</xsl:call-template>
</xsl:variable>
精彩评论