[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Recursive string replace in XSLT 2.0

Subject: Re: Recursive string replace in XSLT 2.0
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Jan 2017 20:43:25 -0000
Re:  Recursive string replace in XSLT 2.0
> On 6 Jan 2017, at 19:41, David Carlisle d.p.carlisle@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> I'd have written it as a function rather than template, but the main
> issue is you want your parameter to be (always) a sequence of elements
> not sometimes a sequence of elements and sometimes a document node
> with a sequence of child elements.

And the best way of ensuring that is to declare the types e.g.

> <xsl:param name="regex" as="element(regex)*/>

which (a) documents what you are expecting for the benefit of the human
reader, (b) gives you a type error if the caller supplies the wrong type of
value, and (c) gives the optimizer more information to work with.

Michael Kay
Saxonica


>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>    exclude-result-prefixes="xs"
>    version="2.0">
>
>    <xsl:output indent="yes"/>
>
>    <xsl:param name="regexes">
>        <regex><find>a</find><change>x</change></regex>
>        <regex><find>b</find><change>y</change></regex>
>        <regex><find>c</find><change>z</change></regex>
>    </xsl:param>
>
>    <xsl:template match="/">
>        <xsl:apply-templates/>
>    </xsl:template>
>
>    <xsl:template match="p">
>        <p><xsl:apply-templates/></p>
>    </xsl:template>
>
>    <xsl:template match="text()"><!--[string-length(.)>0]-->
>        <xsl:message select="."></xsl:message>
>        <xsl:call-template name="applyRegexes">
>            <xsl:with-param name="nodeText" select="."/>
>            <xsl:with-param name="regex" select="$regexes/regex"/>
>         </xsl:call-template>
>    </xsl:template>
>
>    <xsl:template name="applyRegexes">
>        <xsl:param name="nodeText"/>
>        <xsl:param name="regex"/>
>        <xsl:message select="$regex"></xsl:message>
>        <xsl:message select="$regex[1]"/>
>        <xsl:message select="$regex[position()>1]"/>
>        <xsl:choose>
>            <xsl:when test="$regex">
>                <xsl:call-template name="applyRegexes">
>                    <xsl:with-param name="nodeText"
> select="replace($nodeText,$regex[1]/find,$regex[1]/change)"/>
>                    <xsl:with-param name="regex"
> select="$regex[position()>1]"/>
>                </xsl:call-template>
>            </xsl:when>
>            <xsl:otherwise>
>                <xsl:value-of select="$nodeText"/>
>            </xsl:otherwise>
>        </xsl:choose>
>     </xsl:template>
>
> </xsl:stylesheet>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.