This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Replacing all Occurences of a String
- To: XSL List <xsl-list at lists dot mulberrytech dot com>
- Subject: Re: [xsl] Replacing all Occurences of a String
- From: Joerg Pietschmann <joerg dot pietschmann at zkb dot ch>
- Date: Thu, 30 Aug 2001 18:43:24 +0200
- Organization: ZKB
- Reply-To: xsl-list at lists dot mulberrytech dot com
"Roger" <roger@nomad.ch> wrote:
> Hi there
> I want to replace the english months with the german months. It is possible
> that the elements <sfzRelease/> and <sfzProdjahr/> have several months, like
> this:
>
> <sfzRelease>January, February 2001</sfzRelease>
>
> I want at the end:
>
> <sfzRelease>Januar, Februar 2001</sfzRelease>
>
> I took Michael Kay's replace.xsl Stylesheet (Replacing all Occurences of a
> String). But I don't want to give it the parameters in the stylesheet
> itself. I made a xsl:choose with the parameters, but that isn't accepted.
I hope i have understood correctly that you don't want to hardwire
the replacement pairs in the style sheet.
You can build a XML file which holds the pairs and pull it into
the style sheet via document(). Iterating over the replacements
ist awkward, unfortunately.
File replacements.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<replacements>
<replacement>
<from>January</from>
<to>Januar</to>
</replacement>
<replacement>
<from>February</from>
<to>Februar</to>
</replacement>
<!-- and so on -->
</replacements>
Simplified style sheet:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.O" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="do-replace">
<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="do-replace">
<xsl:with-param name="text" select="substring-after($text, $replace)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="sfzRelease | sfzProdjahr">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="process">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="replacements"
select="document('replacements.xml')/replacements/replacement"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="process">
<xsl:with-param name="text"/>
<xsl:with-param name="replacements"/>
<xsl:choose>
<xsl:when test="replacements">
<xsl:call-template name="process">
<xsl:with-param name="text">
<xsl:call-template name="do-replace">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="$replacements[1]/from"/>
<xsl:with-param name="by" select="$replacements[1]/to"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="replacements" select="$replacements[position() > 1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Untested. HTH anyway.
Try an extension for multi-language supprot by yourself.
J.Pietschmann
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list