[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

AW: Replacing all Occurences of a String

Subject: AW: Replacing all Occurences of a String
From: "Roger" <roger@xxxxxxxx>
Date: Thu, 30 Aug 2001 19:32:51 +0200
string process xsl
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@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]Im Auftrag von Joerg
Pietschmann
Gesendet: Donnerstag, 30. August 2001 18:43
An: XSL List
Betreff: Re:  Replacing all Occurences of a String


"Roger" <roger@xxxxxxxx> 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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.