Re: [xsl] construct dynamic replacement value in replace()?

Subject: Re: [xsl] construct dynamic replacement value in replace()?
From: "Birnbaum, David J djbpitt@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 31 Oct 2017 10:40:38 -0000
Dear Martin (cc xsl-list),


Thank you for the quick and helpful response! This does the job elegantly.

Best,

David

________________________________
From: Martin Honnen <martin.honnen@xxxxxx>
Sent: Monday, October 30, 2017 1:47 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] construct dynamic replacement value in replace()?

On 30.10.2017 18:33, Birnbaum, David J djbpitt@xxxxxxxx wrote:
> Dear xsl-list,
>
>
> I know how to accomplish this with XSLT string surgery, but is there an
> XPath or XQuery way to calculate the replacement value of the replace()
> function? The following (broken) XQuery expresses the
> general aspiration, although not the reality:
>
>
> declare function local:stuff($input) {
>       let $result := number($input) + 10
>       return xs:string($result)
>       };
> let $initial := '1 Tim. 4:123'
> return replace($initial, '\d+', local:stuff('$0'))
>
> The desired output would be '11 Tim. 14:133', that is, each sequence of
> digits would be regarded as a discrete decimal numerical value, captured
> as the match with '$0', and passed to the local:stuff() function, where
> it would be converted to a number. augmented by 10, converted back to a
> string (since the replacement part of the replace() function must be a
> string), and returned. The actual output, alas, is 'NaN Tim. NaN:NaN'.
> Before I give up and do it in XSLT, I would be grateful for any pointers
> toward an XPath or XQuery solution.

XPath 3.1 can do it with analyze-string:

let $initial := '1 Tim. 4:123'
return string-join(analyze-string($initial, '[0-9]+')/*!(if
(local-name() eq 'match') then . + 10 else string()))

Current Thread