note 97468 deleted from xsltprocessor.registerphpfunctions by danbrown
| From: | danbrown@php.net | Date: | Wed, 21 Apr 2010 18:40:26 +0000 |
| Subject: | note 97468 deleted from xsltprocessor.registerphpfunctions by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168483@lists.php.net to get a copy of this message | ||
Note Submitter: Tom
----
The missing string-replace function in XSLT 1.0 is an all-time classic. You may either use
PHP's str_replace function or (if you wish to stay language-independent) use an XSLT 1.0
implementation:
<xsl:template name="str_replace">
<xsl:param name="subject"/>
<xsl:param name="search"/>
<xsl:param name="replace"/>
<xsl:choose>
<xsl:when test="contains($subject, $search)">
<xsl:value-of select="substring-before($subject, $search)"/>
<xsl:copy-of select="$replace"/>
<xsl:call-template name="str_replace">
<xsl:with-param name="subject" select="substring-after($subject,
$search)"/>
<xsl:with-param name="search" select="$search"/>
<xsl:with-param name="replace" select="$replace" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$subject" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This template is to be called as follows:
<xsl:call-template name="str_replace">
<xsl:with-param name="subject">The string being searched and replaced on,
otherwise known as the haystack.</xsl:with-param>
<xsl:with-param name="search">The value being searched for, otherwise known as the
needle.</xsl:with-param>
<xsl:with-param name="replace">The replacement value that replaces found search
values.</xsl:with-param>
</xsl:call-template>