www.flickr.com
This is a Flickr badge showing public photos and videos from bryanrobson. Make your own badge here.

Misc Templates

Search and Replace

No doubt these are available all over the web, but here are my templates for doing a search and replace in XSL. There are two version of this template, one which includes a “disable-output-escaping” directive.

<!-- ****************************************************************** -->
<!-- template that does a search & replace -->
<xsl:template name="replace-text">

<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" disable-output-escaping="yes"/>
<xsl:call-template name="replace-text">
<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>

<!-- ****************************************************************** -->
<!-- template that does a search & replace -->
<xsl:template name="replace-text-no-doe">

<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="replace-text">
<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>


Upper case a string

Upper case a string – the lower case equivalent should be fairly obvious! This will, of course, only work with the characters included in the list.

<xsl:template name="UpperCase">

<xsl:param name="sInput"/>

<xsl:value-of select="translate(
$sInput,
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

</xsl:template>