Workaround for XQuery Update corruption problem, pending Saxon maintenance release http://markmail.org/message/b27lxtwevs744e62
<result>
<original>
<ref xmlns="http://www.tei-c.org/ns/1.0" xmlns:frus="http://history.state.gov/frus/ns/1.0">
<hi rend="italic">Foreign Relations,</hi> 1952–1954, vol. VII, Part 2 </ref>, p.
1914.</original>
<analysis>
<fn:analyze-string-result xmlns:fn="http://www.w3.org/2005/xpath-functions">
<fn:match>
<fn:group nr="1">, p. </fn:group>
<fn:group nr="2">1914</fn:group>
<fn:group nr="3">.</fn:group>
</fn:match>
</fn:analyze-string-result>
</analysis>
<new>
<ref xmlns="http://www.tei-c.org/ns/1.0">
<hi xmlns:frus="http://history.state.gov/frus/ns/1.0" rend="italic">Foreign
Relations,</hi> 1952–1954, vol. VII, Part 2 , p. 1914</ref>.</new>
</result>
<note xmlns="http://www.tei-c.org/ns/1.0" xmlns:frus="http://history.state.gov/frus/ns/1.0"> For
text of NSC 164/1, see <ref>
<hi rend="italic">Foreign Relations,</hi> 1952–1954, vol. VII, Part 2 </ref>, p.
1914.</note>
xquery version "3.0";
(:
Same as https://gist.github.com/joewiz/2369367de3babba30e0aad8c9beec893, except I've removed local:reconstruct().
Note that unwanted namespace declarations appear inline in the result - see 03-test-results.xml line 17.
:)
declare namespace tei="http://www.tei-c.org/ns/1.0";
let $doc := doc('02-sample.xml')
let $refs := $doc//tei:ref
[matches(following-sibling::node()[1][. instance of text()], '^, pp?\.\s+\d+')]
for $ref in $refs
let $following-text := $ref/following-sibling::text()[1]
let $analyze := analyze-string($following-text, '^(, pp?\.\s+)(\d+)(.*)$')
let $new-ref :=
(
element
{ QName('http://www.tei-c.org/ns/1.0', 'ref') }
{
$ref/node(),
string-join($analyze/fn:match/fn:group[@nr = (1, 2)])
}
)
let $new-following-text := string-join($analyze/fn:match/fn:group[@nr ge 3])
let $test :=
<result>
<original>{$ref, $following-text}</original>
<analysis>{$analyze}</analysis>
<new>{$new-ref, $new-following-text}</new>
</result>
return
(:
$test
:)
(
replace node $ref with $new-ref
,
replace node $following-text with $new-following-text
)