Re: Find/replace algorithm

"Martin Honnen [email protected]" <[email protected]> Wed, 24 Mar 2021 20:38:21 -0000
Newsgroups gmane.text.xml.xsl.general.mulberrytech
Organization Liberty Development
Message-ID <[email protected]>
On 24.03.2021 21:28, [email protected] wrote:
> Hello All,
>
> I have a fairly large XML file similar to this:
>
> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
>
> <products>
>
>  =A0=A0=A0 <product>ACME Wid Assbly</product>
>
>  =A0=A0=A0 <product>Ford Eng Rebuild Kit</product>
>
> </products>
>
> I want to do an identity transform except that I want to do some find
> and replace on some of the words. For example
>
> Wid =3D Widget
>
> Assbly =3D Assembly
>
> Eng =3D Engine
>
> I am thinking of creating a lookup XML file to drive the find/replace
> actions:
>
> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
>
> <lookup>
>
>  =A0=A0=A0 <entry find=3D"\bWid\b" replace=3D"Widget"/>
>
>  =A0=A0=A0 <entry find=3D"\bAssbly\b" replace=3D"Assembly"/>
>
>  =A0=A0=A0 <entry find=3D"\bEng\b" replace=3D"Engine"/>
>
> </lookup>
>
> I am having trouble figuring out a good XSLT 2 or 3 algorithm for
> actually doing the replacements. Any suggestions or pointers would be
> appreciated. Thank you very much.

Perhaps using fold-left or xsl:iterate for the replace, together with
the ;j or ;n flag to enable support for \b:


   <xsl:param name=3D"lookup">
<lookup>

     <entry find=3D"\bWid\b" replace=3D"Widget"/>

     <entry find=3D"\bAssbly\b" replace=3D"Assembly"/>

     <entry find=3D"\bEng\b" replace=3D"Engine"/>

</lookup>
   </xsl:param>

   <xsl:mode on-no-match=3D"shallow-copy"/>


   <xsl:template match=3D"product/text()">
       <xsl:iterate select=3D"$lookup/lookup/entry">
           <xsl:param name=3D"text" select=3D"."/>
           <xsl:on-completion select=3D"$text"/>
           <xsl:next-iteration>
               <xsl:with-param name=3D"text" select=3D"replace($text, @find,
@replace, ';j')"/>
           </xsl:next-iteration>
       </xsl:iterate>
   </xsl:template>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/3329386
or by email: [email protected]
--~--