[xquery-talk] Fuctions and variables in replacement string of fn:replace()
Michael Kay
mike at saxonica.com
Wed May 1 01:02:07 PDT 2013
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
>> effect.
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
Saxonica
More information about the talk
mailing list