This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

AW: Replacing all Occurences of a String


Hi
I tried it out, but had this message:

XSL element 'template' cannot contain element 'with-param' at this point.

Any hints?
Thanx anyway
Best regards
Roger



-----Ursprüngliche Nachricht-----
Von: owner-xsl-list@lists.mulberrytech.com
[mailto:owner-xsl-list@lists.mulberrytech.com]Im Auftrag von Joerg
Pietschmann
Gesendet: Donnerstag, 30. August 2001 18:43
An: XSL List
Betreff: Re: [xsl] Replacing all Occurences of a String


"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() &gt; 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


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]