[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
[Recent Entries]
[Reply To This Message]
Re: Find/replace algorithm
On 24.03.2021 21:38, Martin Honnen martin.honnen@xxxxxx wrote:
On 24.03.2021 21:28, rick@xxxxxxxxxxxxxx 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:
The fold-left might be more compact but requires Saxon 10 HE or PE or EE
for earlier versions:
<xsl:template match="product/text()">
<xsl:value-of select="fold-left($lookup/lookup/entry, .,
function($w, $e) { replace($w, $e/@find, $e/@replace, ';j') })"/>
</xsl:template>
|
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format
RSS 2.0 |
|
Atom 0.3 |
|
|