[prev in list] [next in list] [prev in thread] [next in thread]
List: xsl-list
Subject: Re: [xsl] Recursive string replace in XSLT 2.0
From: "Rick Quatro rick () rickquatro ! com" <xsl-list-service () lists ! mulberrytech ! com>
Date: 2017-01-06 22:27:37
Message-ID: 20170106172658.33855 () lists ! mulberrytech ! com
[Download RAW message or body]
Hi David,
OK, I get it now. Thank you for clarifying.
Rick
From: David Carlisle d.p.carlisle@gmail.com \
[mailto:xsl-list-service@lists.mulberrytech.com]
Sent: Friday, January 06, 2017 4:41 PM
To: xsl-list@lists.mulberrytech.com
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@rickquatro.com \
<xsl-list-service@lists.mulberrytech.com> 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>"(\S)</find><change>“$1</change></regex>
<regex><find>(\S)"</find><change>$1”</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">"Abcdefghij."</p></test>
<test><p class="Note">"defghij."</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@saxonica.com
[mailto:xsl-list-service@lists.mulberrytech.com]
Sent: Friday, January 06, 2017 4:25 PM
To: xsl-list@lists.mulberrytech.com
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@gmail.com
<xsl-list-service@lists.mulberrytech.com> 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 <> )
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/651070
or by email: xsl-list-unsub@lists.mulberrytech.com
--~--
[Attachment #3 (text/html)]
<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type \
content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 12 \
(filtered medium)"><style><!-- /* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{mso-style-priority:99;
mso-style-link:"Balloon Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:8.0pt;
font-family:"Tahoma","sans-serif";}
span.gmail-im
{mso-style-name:gmail-im;}
span.gmail-
{mso-style-name:gmail-;}
span.hoenzb
{mso-style-name:hoenzb;}
span.im
{mso-style-name:im;}
span.EmailStyle21
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
span.BalloonTextChar
{mso-style-name:"Balloon Text Char";
mso-style-priority:99;
mso-style-link:"Balloon Text";
font-family:"Tahoma","sans-serif";}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div \
class=WordSection1><p class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Hi \
David,<o:p></o:p></span></p><p class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>OK, I get \
it now. Thank you for clarifying.<o:p></o:p></span></p><p class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Rick<o:p></o:p></span></p><p \
class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div \
style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p \
class=MsoNormal><b><span \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> David Carlisle \
d.p.carlisle@gmail.com [mailto:xsl-list-service@lists.mulberrytech.com] \
<br><b>Sent:</b> Friday, January 06, 2017 4:41 PM<br><b>To:</b> \
xsl-list@lists.mulberrytech.com<br><b>Subject:</b> Re: [xsl] Recursive string replace \
in XSLT 2.0<o:p></o:p></span></p></div><p \
class=MsoNormal><o:p> </o:p></p><div><div><div><div><div><p class=MsoNormal \
style='margin-bottom:12.0pt'>not here<br><br> <xsl:param \
name="regexes" as="element(regex)*"><o:p></o:p></p></div><p \
class=MsoNormal style='margin-bottom:12.0pt'>Michael meant here, in the template \
definition<br><br><span class=gmail-im> <xsl:param \
name="regex"/></span><br><br><o:p></o:p></p></div><p class=MsoNormal \
style='margin-bottom:12.0pt'><span class=gmail-im>You could declare your top level \
initialiser to also be a sequence of elements but then your initial call would have \
to be</span><br><br><br><span class=gmail-im> \
<xsl:with-param name="regex" \
select="$regexes"/></span><o:p></o:p></p></div><p class=MsoNormal \
style='margin-bottom:12.0pt'><span class=gmail->not</span><br><br><span \
class=gmail-im> <xsl:with-param \
name="regex" \
select="$regexes/regex"/></span><o:p></o:p></p></div><p class=MsoNormal \
style='margin-bottom:12.0pt'><span \
class=gmail-im>David</span><o:p></o:p></p></div><div><p \
class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>On 6 January 2017 at \
21:37, Rick Quatro <a href="mailto:rick@rickquatro.com">rick@rickquatro.com</a> \
<<a href="mailto:xsl-list-service@lists.mulberrytech.com" \
target="_blank">xsl-list-service@lists.mulberrytech.com</a>> \
wrote:<o:p></o:p></p><p class=MsoNormal style='margin-bottom:12.0pt'>Thank you \
Michael. Oddly enough, when I add the as="element(regex)*" \
the<br>finds/changes fail. Here is the entire stylesheet:<br><br><?xml \
version="1.0" encoding="UTF-8"?><br><xsl:stylesheet \
xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform" \
target="_blank">http://www.w3.org/1999/XSL/Transform</a>"<br> \
xmlns:xs="<a href="http://www.w3.org/2001/XMLSchema" \
target="_blank">http://www.w3.org/2001/XMLSchema</a>"<br> \
exclude-result-prefixes="xs"<br> \
version="2.0"><br><br> <xsl:output \
indent="yes"/><br><br> <xsl:param \
name="regexes" as="element(regex)*"><br> \
<regex><find>&quot;(\S)</find><change>&#8220;$1</change></regex><br> \
\
<regex><find>(\S)&quot;</find><change>$1&#8221;</change></regex><br> \
</xsl:param><br><br> <xsl:template \
match="/"><br> \
<xsl:apply-templates/><br> </xsl:template><br><br> \
<xsl:template match="p"><br> \
<p><xsl:apply-templates/></p><br> \
</xsl:template><br><br> <xsl:template \
match="text()[string-length(normalize-space(.))>0]"><br> \
<xsl:call-template name="applyRegexes"><br> \
<xsl:with-param name="nodeText" \
select="."/><br> \
<xsl:with-param name="regex" \
select="$regexes/regex"/><br> \
</xsl:call-template><br> </xsl:template><br><br> \
<xsl:template name="applyRegexes"><br> \
<xsl:param name="nodeText"/><br> \
<xsl:param name="regex"/><br> \
<xsl:choose><br> <xsl:when \
test="$regex"><br> \
<xsl:variable name="temp"><br> \
\
<xsl:value-of<br>select="replace($nodeText,$regex[1]/find,$regex[1]/change)"/><br> \
</xsl:variable><br> \
<xsl:call-template \
name="applyRegexes"><br> \
<xsl:with-param name="nodeText" \
select="$temp"/><br> \
<xsl:with-param \
name="regex"<br>select="$regex[position()>1]"/><br> \
</xsl:call-template><br> \
</xsl:when><br> \
<xsl:otherwise><br> \
<xsl:value-of select="$nodeText"/><br> \
</xsl:otherwise><br> \
</xsl:choose><br> \
</xsl:template><br><br></xsl:stylesheet><br><br>Here is my \
input:<br><br><?xml version='1.0' encoding='UTF-8'?><br><root><br> \
<test><p \
class="Note">&quot;Abcdefghij.&quot;</p></test><br> \
<test><p \
class="Note">&quot;defghij.&quot;</p></test><br></root><br><br>Without \
the as attribute, the finds/changes are applied, but with it, they<br>don't. \
Thanks.<br><span style='color:#888888'><br><span \
class=hoenzb>--Rick</span><br></span><br><br><span class=im>-----Original \
Message-----</span><br><span class=im>From: Michael Kay <a \
href="mailto:mike@saxonica.com">mike@saxonica.com</a></span><br><span \
class=im>[mailto:<a href="mailto:xsl-list-service@lists.mulberrytech.com">xsl-list-service@lists.mulberrytech.com</a>]</span><br><span \
class=im>Sent: Friday, January 06, 2017 4:25 PM</span><br><span class=im>To: <a \
href="mailto:xsl-list@lists.mulberrytech.com">xsl-list@lists.mulberrytech.com</a></span><br><span \
class=im>Subject: Re: [xsl] Recursive string replace in XSLT \
2.0</span><o:p></o:p></p><div><div><p class=MsoNormal style='margin-bottom:12.0pt'>If \
you don't like head-tail recursion for this kind of problem, there are a<br>couple of \
alternatives you might consider.<br><br>One is xsl:iterate, which looks something \
like this:<br><br><xsl:iterate \
select="$list-of-replacements"><br> <xsl:param \
name="str" as="xs:string"/><br> <xsl:on-completion \
select="$str"/><br> <xsl:next-iteration><br> \
<xsl:with-param name="str" select="replace($str, find, \
change)"/><br> \
</xsl:next-iteration><br></xsl:iterate><br><br>The other is \
fold-left:<br><br>fold-left($list-of-replacements, $str, function($str, $regex) \
{<br>replace($str, $regex/find, $regex/change) } )<br><br>Both these require \
3.0.<br><br>Michael Kay<br>Saxonica<br><br><br>> On 6 Jan 2017, at 19:41, David \
Carlisle <a href="mailto:d.p.carlisle@gmail.com">d.p.carlisle@gmail.com</a><br><<a \
href="mailto:xsl-list-service@lists.mulberrytech.com">xsl-list-service@lists.mulberrytech.com</a>> \
wrote:<br>><br>> I'd have written it as a function rather than template, but \
the main<br>> issue is you want your parameter to be (always) a sequence of \
elements<br>> not sometimes a sequence of elements and sometimes a document \
node<br>> with a sequence of child elements.<br>><br>> <?xml \
version="1.0" encoding="UTF-8"?> <xsl:stylesheet<br>> \
xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform" \
target="_blank">http://www.w3.org/1999/XSL/Transform</a>"<br>> \
xmlns:xs="<a href="http://www.w3.org/2001/XMLSchema" \
target="_blank">http://www.w3.org/2001/XMLSchema</a>"<br>> \
exclude-result-prefixes="xs"<br>> \
version="2.0"><br>><br>> <xsl:output \
indent="yes"/><br>><br>> <xsl:param \
name="regexes"><br>> \
<regex><find>a</find><change>x</change></regex><br>> \
\
<regex><find>b</find><change>y</change></regex><br>> \
\
<regex><find>c</find><change>z</change></regex><br>> \
</xsl:param><br>><br>> <xsl:template \
match="/"><br>> \
<xsl:apply-templates/><br>> \
</xsl:template><br>><br>> <xsl:template \
match="p"><br>> \
<p><xsl:apply-templates/></p><br>> \
</xsl:template><br>><br>> <xsl:template \
match="text()"><!--[string-length(.)>0]--><br>> \
<xsl:message \
select="."></xsl:message><br>> \
<xsl:call-template name="applyRegexes"><br>> \
<xsl:with-param name="nodeText" \
select="."/><br>> \
<xsl:with-param name="regex" \
select="$regexes/regex"/><br>> \
</xsl:call-template><br>> \
</xsl:template><br>><br>> <xsl:template \
name="applyRegexes"><br>> <xsl:param \
name="nodeText"/><br>> <xsl:param \
name="regex"/><br>> <xsl:message \
select="$regex"></xsl:message><br>> \
<xsl:message select="$regex[1]"/><br>> \
<xsl:message select="$regex[position()>1]"/><br>> \
<xsl:choose><br>> \
<xsl:when test="$regex"><br>> \
<xsl:call-template \
name="applyRegexes"><br>> \
<xsl:with-param name="nodeText"<br>> \
select="replace($nodeText,$regex[1]/find,$regex[1]/change)"/><br>> \
<xsl:with-param \
name="regex"<br>> \
select="$regex[position()>1]"/><br>> \
</xsl:call-template><br>> \
</xsl:when><br>> \
<xsl:otherwise><br>> \
<xsl:value-of select="$nodeText"/><br>> \
</xsl:otherwise><br>> \
</xsl:choose><br>> </xsl:template><br>><br>> \
</xsl:stylesheet><br>><o:p></o:p></p></div></div></div><p \
class=MsoNormal><o:p> </o:p></p></div><div><div \
style='border:none;border-top:solid black 1.0pt;padding:4.0pt 0in 0in \
0in;margin-top:5.0pt;margin-bottom:5.0pt'><p class=MsoNormal align=center \
style='text-align:center;background:#DDDDDD'><span \
style='font-size:10.0pt;font-family:"Arial","sans-serif";color:#888888'><a \
href="http://www.mulberrytech.com/xsl/xsl-list">XSL-List info and archive</a> \
<o:p></o:p></span></p><p class=MsoNormal align=center \
style='text-align:center;background:#DDDDDD'><span \
style='font-size:10.0pt;font-family:"Arial","sans-serif";color:#888888'><a \
href="-list/612310">EasyUnsubscribe</a> (<a href="">by email</a>) \
<o:p></o:p></span></p></div></div></div></body></html> <div><!-- begin \
bl.html.trailer --> <div style="border-top:1px solid black; background-color: \
#dddddd;
color: #888888; font-size: smaller; padding: 5px; text-align: center;
font-family: arial,verdana,arial,sans-serif; margin-top:1em; clear:
both; margin: auto">
<a href="http://www.mulberrytech.com/xsl/xsl-list">
XSL-List info and archive</a>
<div style="text-align:center;">
<a style="color: blue;"
href="http://lists.mulberrytech.com/unsub/xsl-list/651070"
> EasyUnsubscribe</a>
(<a style="color: blue;"
href="mailto:xsl-list-unsub@lists.mulberrytech.com?subject=remove"
> by email</a>)
</div>
</div>
<!-- end bl.html.trailer --></div>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic