[xquery-talk] Fuctions and variables in replacement string of fn:replace()
dsewell at virginia.edu
Wed May 1 06:43:00 PDT 2013
Thanks, Michael, for the clarification. I'm aware that fn:analyze-string is
(with XPath 3.0) the right tool for this kind of job, I was just confused about
the extent to which fn:replace() was capable of being a poor-man's equivalent.
The answer then is "not much".
On Wed, 1 May 2013, Michael Kay wrote:
> On 1 May 2013, at 04:14, Liam R E Quin wrote:
>> 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?
> Yes. XQuery is an orthogonal language, any expression can be replaced by a different expression that returns the same value.
>>> 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"))
>>> ==> Now, let's SENDSEND OUTOUT for QUICHEQUICHE!!
>>> but in other cases it doesn't. For example, lower-case("$0") has no
> It should have no effect. It's the same as replace($string, $match, '$0') which replaces every string that matches $match with itself.
>>> And substring("$0", 1, 1) throws an error.
> Because '$' is not a valid replacement string.
> OK, I think I've understood what you wanted (maybe!). You didn't want to construct the replacement string using a function, you wanted to apply a function to the matching substrings found by the matching operation: you want a higher-order replace(). Saxon has such a thing in the form of saxon:analyze-string (see http://www.saxonica.com/documentation/#!functions/saxon/analyze-string) but in 3.0 the WG took a different direction in its definition of fn:analyze-string(), which is probably easier for ordinary mortals to cope with: http://www.w3.org/TR/xpath-functions-30/#func-analyze-string
> Michael Kay
> talk at x-query.com
David Sewell, Editorial and Technical Manager
ROTUNDA, The University of Virginia Press
PO Box 400314, Charlottesville, VA 22904-4314 USA
Email: dsewell at virginia.edu Tel: +1 434 924 9973
More information about the talk