xsl-list
[Top] [All Lists]

Re: [xsl] Multiple replace() in XSLT 2

2019-05-16 14:16:51
On 16.05.2019 20:59, Rick Quatro rick(_at_)rickquatro(_dot_)com wrote:

I have a look up file of find/change pairs that I have to apply to a
text node in my XML document. I am using XSLT 2. Here is an example of
the lookup file:

<?xml version="1.0" encoding="UTF-8"?>
<findchange_lookup>
<findchangefind="Eicas"change="EICAS"/>
<findchangefind="Ulb"change="ULB"/>
</findchange_lookup>

I am reading this in as a global variable, but I am not sure the best
approach for doing multiple replacements on the node. I can use
recursion like in XSLT 1, but I can't think of how to do this in XSLT 2.

I don't understand why you say you know recursion in XSLT 1 but can't do
it in XSLT 2.

There could be any number of <findchange> elements in my lookup file.
Any pointers would be appreciated. Thank you very much.

The examples seem to "upper case" the search strings so doing e.g.

  <xsl:variable name="pattern" as="xs:string"
select="string-join(doc('lookup.xml')/findchange_lookup/findchange/@find,
'|')"/>

  <xsl:template match="foo/text()">
    <xsl:analyze-string select="." regex="{$pattern}">
       <xsl:matching-substring>
           <xsl:value-of select="upper-case(.)"/>
       </xsl:matching-substring>
       <xsl:non-matching-substring>
           <xsl:value-of select="."/>
       </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>

Or declare a key

   <xsl:key name="lookup" match="findchange" use="@find"/>

and

  <xsl:template match="foo/text()">
    <xsl:analyze-string select="." regex="{$pattern}">
       <xsl:matching-substring>
           <xsl:value-of select="key('lookup', .,
doc('lookup.xml'))/@change"/>
       </xsl:matching-substring>
       <xsl:non-matching-substring>
           <xsl:value-of select="."/>
       </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>

Not sure if that suffices or if you need to replace one term recursively
in the replacement of a previous term.

And of course depending on the "find" values, to build the regular
expression pattern, you might need to escape metacharacters first, I
think functx has a function for that.
--~----------------------------------------------------------------
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>