Re: [xsl] Recursive string replace in XSLT 2.0

Subject: Re: [xsl] Recursive string replace in XSLT 2.0
From: "Rick Quatro rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Jan 2017 22:26:59 -0000
Hi David,



OK, I get it now. Thank you for clarifying.



Rick



From: David Carlisle d.p.carlisle@xxxxxxxxx
[mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
Sent: Friday, January 06, 2017 4:41 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Recursive string replace in XSLT 2.0



not here

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

Michael meant here, in the template definition

        <xsl:param name="regex"/>



You could declare your top level initialiser to also be a sequence of elements
but then your initial call would have to be


            <xsl:with-param name="regex" select="$regexes"/>

not

            <xsl:with-param name="regex" select="$regexes/regex"/>

David



On 6 January 2017 at 21:37, Rick Quatro rick@xxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

Thank you Michael. Oddly enough, when I add the as="element(regex)*" the
finds/changes fail. Here is the entire stylesheet:

<?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" as="element(regex)*">
        <regex><find>&quot;(\S)</find><change>&#8220;$1</change></regex>
        <regex><find>(\S)&quot;</find><change>$1&#8221;</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(normalize-space(.))>0]">
        <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:choose>
            <xsl:when test="$regex">
                <xsl:variable name="temp">
                    <xsl:value-of
select="replace($nodeText,$regex[1]/find,$regex[1]/change)"/>
                </xsl:variable>
                <xsl:call-template name="applyRegexes">
                    <xsl:with-param name="nodeText" select="$temp"/>
                    <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>

Here is my input:

<?xml version='1.0' encoding='UTF-8'?>
<root>
   <test><p class="Note">&quot;Abcdefghij.&quot;</p></test>
   <test><p class="Note">&quot;defghij.&quot;</p></test>
</root>

Without the as attribute, the finds/changes are applied, but with it, they
don't. Thanks.

--Rick


-----Original Message-----
From: Michael Kay mike@xxxxxxxxxxxx
[mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
Sent: Friday, January 06, 2017 4:25 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Recursive string replace in XSLT 2.0

If you don't like head-tail recursion for this kind of problem, there are a
couple of alternatives you might consider.

One is xsl:iterate, which looks something like this:

<xsl:iterate select="$list-of-replacements">
  <xsl:param name="str" as="xs:string"/>
  <xsl:on-completion select="$str"/>
  <xsl:next-iteration>
    <xsl:with-param name="str" select="replace($str, find, change)"/>
  </xsl:next-iteration>
</xsl:iterate>

The other is fold-left:

fold-left($list-of-replacements, $str, function($str, $regex) {
replace($str, $regex/find, $regex/change) } )

Both these require 3.0.

Michael Kay
Saxonica


> 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.
>
> <?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>
>



XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>

EasyUnsubscribe <-list/612310>  (by email <> )

Current Thread