xsl-list
[Top] [All Lists]

Re: [xsl] Find/replace algorithm

2021-03-24 15:38:20
On 24.03.2021 21:28, rick(_at_)rickquatro(_dot_)com wrote:
Hello All,

I have a fairly large XML file similar to this:

<?xml version="1.0" encoding="UTF-8"?>

<products>

     <product>ACME Wid Assbly</product>

     <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 = Widget

Assbly = Assembly

Eng = Engine

I am thinking of creating a lookup XML file to drive the find/replace
actions:

<?xml version="1.0" encoding="UTF-8"?>

<lookup>

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

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

     <entry find="\bEng\b" replace="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="lookup">
<lookup>

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

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

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

</lookup>
  </xsl:param>

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


  <xsl:template match="product/text()">
      <xsl:iterate select="$lookup/lookup/entry">
          <xsl:param name="text" select="."/>
          <xsl:on-completion select="$text"/>
          <xsl:next-iteration>
              <xsl:with-param name="text" select="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/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--


<Prev in Thread] Current Thread [Next in Thread>