<div dir="ltr">Hi Joe,<br><br>I'm not sure what's happening with the write-back, but looking at your code, it seems that the reconstruct function is not needed.<div><br></div><div>Indeed, when you write, say:<br><br>  replace node $ref with $new-ref<br><br>A copy of $new-ref is always made (this the semantics of XQuery Update), which is what is inserted. So even if $new-ref points to a node in the original document, this alone can't lead to an error.<br><br>The same goes line 43: a copy is made, that's two in total.</div><div><br></div><div>From what I see, an insert-as-last into the ref element would be sufficient.</div><div><br></div><div>I hope it helps!</div><div><div><br></div><div>Kind regards,</div><div>Ghislain</div><div><br><br>On Fri, Jun 10, 2016 at 12:53 AM, Joe Wicentowski <<a href="mailto:joewiz@gmail.com">joewiz@gmail.com</a>> wrote:<br>><br>> Hi all,<br>><br>> I'm struggling with an XQuery Update problem which is corrupting the file I'm trying to update.  I've posted my code samples to <a href="https://gist.github.com/joewiz/2369367de3babba30e0aad8c9beec893">https://gist.github.com/joewiz/2369367de3babba30e0aad8c9beec893</a> - but here's the core of the issue:<br>><br>> I'm working with a TEI document containing <ref> elements that I'm trying to manipulate with XQuery Update. Specifically, I'm grabbing a page number reference from the text node immediately following the <ref> element, and I want to move the page number inside the <ref> element. (I'm actually trying to do a few more things, but I've reduced the sample code to do just this, because it illustrates the problem I'm facing.)<br>><br>> The problem is that the XQuery Update statement corrupts my file. The resulting file has 0 bytes. When I comment out the XQuery Update statement and uncomment the $test variable in the return expression, I get expected results, so I think the logic is sound. I have a feeling that the problem may have to do with line 25, where I add attributes to an element; when I comment out this, the corruption doesn't occur. But I need the attributes in that line. So I'm stumped. I've never encountered data corruption with XQuery Update, so I really hope there's a solution.<br>><br>> I'm using Saxon-EE XQuery 9.6.0.7 inside of oXygen 17.1, with Saxon's options for XQuery 3.0 and XQuery Update enabled.<br>><br>> Many thanks for any suggestions,<br>> Joe<br>><br>> _______________________________________________<br>> <a href="mailto:talk@x-query.com">talk@x-query.com</a><br>> <a href="http://x-query.com/mailman/listinfo/talk">http://x-query.com/mailman/listinfo/talk</a></div></div></div>