Re: [xsl] Replace the portion of text that matches pattern: XPath versus SNOBOL

Subject: Re: [xsl] Replace the portion of text that matches pattern: XPath versus SNOBOL
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 22 Mar 2025 12:28:10 -0000
On 22/03/2025 13:03, Roger L Costello costello@xxxxxxxxx wrote:
Suppose that you want to only replace the first match, i.e., we want
"BELANCE." I do not think that there is any way to configure the XPath
replace() function to "Replace only the first match."


It might be a bit convoluted but if you use analyze-string in XPath 3.1 you can then process only the first match with e.g.

B  analyze-string('BALANCE', 'A')!*!(if (. is ../*:match[1]) then
replace(., 'A', 'E') else .)!data()=>string-join()

Within XSLT it might be cleaner to use xsl:function to implement that
with either xsl:analyze-string or with analyze-string and pushing its
result through templates that then match on the first match to perform
the replacement.

Current Thread