xml - Replace string using XSL 1.0 -
i'm having problems replacing string within string using xsl 1.0. i've read various threads on site , others can't put finger on why method not work. purposes of question i've dumbed down data little you'll jist, hopefully.
so, i've got xml:
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="replaceastring.xsl"?> <body> <astring>texttoreplace</astring> </body>
and xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://w3.org/1999/xsl/transform"> <xsl:template match="/"> <h1> <xsl:value-of select="$myvar"/> </h1> </xsl:template> <xsl:variable name="myvar"> <xsl:call-template name="string-replace-all"> <xsl:with-param name="text" select="body/astring/" /> <xsl:with-param name="replace" select="'texttoreplace'" /> <xsl:with-param name="by" select="'withthistext'" /> </xsl:call-template> </xsl:variable> <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:with-param name="text" select="substring-after($text,$replace)"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text" /> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
i hoping result of transformation text "withthistext" appearing within heading1 html tag. unfortunately i'm getting nothing back. 1 of things can't seem figure out wrong logic. i'm no xml/xsl professional i'm not complete novice either. although i'm sure pull me on :p
any appreciated! i'm stuck here :(
many thanks,
james
first thing, change:
<xsl:with-param name="text" select="body/astring/" />
to:
<xsl:with-param name="text" select="body/astring" />
next, note within first <xsl:when>
have:
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
i believe needs part of <xsl:call-template>
?
Comments
Post a Comment