[xquery-talk] Fuctions and variables in replacement string of fn:replace()

Liam R E Quin liam at w3.org
Tue Apr 30 20:14:27 PDT 2013

On Tue, 2013-04-30 at 23:01 -0400, David Sewell wrote:
> When using fn:replace(), is it kosher or illegal to construct the
> replacement string dynamically using a function? In some cases this
> seems to work (tested in Saxon and MarkLogic):
> xquery version "1.0";
> let $string := "Now, let's SEND OUT for QUICHE!!"
> let $match := "[A-Z][A-Z]+"
> return replace($string, $match, concat("$0","$0"))
> but in other cases it doesn't. For example, lower-case("$0") has no
> effect. And substring("$0", 1, 1) throws an error.

In your first example,
return replace($string, $match, concat("$0","$0"))
the replace function gets called with 3 arguments:
      "Now, let's SEND OUT for QUICHE!!",

Now, replace() treats $0 as whatever matched - it is not a variable
reference, even though it looks like one. Th $ sign is special to

However, lower-case("$0") will return "$0" since neither $ nor 0 is an
upper case letter.

substring("$0", 1, 1) ought to return a $ sign, and if you use that as
an argument to replace(0 you'll get an error, because replace() wants
dollar signs to be followed by digits.

Hope that helps.


Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org freenode/#xml

More information about the talk mailing list