Updating XML with XQuery 1.0
I was reading an interesting discussion yesterday on xquery-talk, replacing a node in in-memory XML.How can one modify an XML structure through XQuery? In the future, the answer is definitely XQuery Update Facility. But the XQuery Update Facility is currently still work in progress, and not yet widely supported. What do we do today?Ryan Grimm wrote an XQuery library to update an in-memory XML structure. And it looks like the in-mem-update library is pretty functional complete, having the following functions.
This shows one of the possible issues with the library. Each modification made to an XML structure results in a copy. Making a lot of changes to a single XML structure, or updating a huge XML structure might affect performance. Still, I believe the library is useful in a lot of common scenarios.The library is written to be used with MarkLogic Server, and unfortunately based on an older version of the XQuery specification. This makes it fail out of the box using XQuery 1.0 compliant processors. I updated the XQuery module in order to make it XQuery 1.0 compatible, and in addition added support for document nodes. It's available for download here.So, you can now update all your data with DataDirect XQuery. Using the ddtek:sql-insert, ddtek:sql-update and ddtek:sql-delete functions you can update your relation database. And using the in-mem-update library you can now also make changes to your XML documents.I believe this library is complementary to the functions modifying XML elements and attributes available in the FunctX XQuery library. Wouldn't it be cool to have these functions added to FunctX? I leave it to Ryan Grimm and Priscilla Walmsley to discuss this in detail.
- node-insert-child
- node-insert-before
- node-insert-after
- node-replace
- node-delete
let $uid :=Using the library we end up doing something as follows,
doc("users.xml")/users/user_tuple[name="Annabel Lee"]/userid
return do
insert
<bid_tuple>
<userid>{data($uid)}</userid>
<itemno>1001</itemno>
<bid>60</bid>
<bid_date>1999-02-01</bid_date>
<bid_tuple>
into doc("bids.xml")/bids
import module namespace mem = "http://xqdev.com/in-mem-update" at "in-mem-update.xqy";Looks pretty similar, no? There is actually one fundamental difference. With the XQuery Update Facility, the bids.xml document is actually updated. The in-mem-update variant, doesn't update the bids.xml document, but rather returns a copy of the original document reflecting the change.
let $uid :=
doc("users.xml")/users/user_tuple[name="Annabel Lee"]/userid
return
mem:node-insert-child(
doc("bids.xml")/bids,
<bid_tuple>
<userid>{data($uid)}</userid>
<itemno>1001</itemno>
<bid>60</bid>
<bid_date>1999-02-01</bid_date>
</bid_tuple>)
This shows one of the possible issues with the library. Each modification made to an XML structure results in a copy. Making a lot of changes to a single XML structure, or updating a huge XML structure might affect performance. Still, I believe the library is useful in a lot of common scenarios.The library is written to be used with MarkLogic Server, and unfortunately based on an older version of the XQuery specification. This makes it fail out of the box using XQuery 1.0 compliant processors. I updated the XQuery module in order to make it XQuery 1.0 compatible, and in addition added support for document nodes. It's available for download here.So, you can now update all your data with DataDirect XQuery. Using the ddtek:sql-insert, ddtek:sql-update and ddtek:sql-delete functions you can update your relation database. And using the in-mem-update library you can now also make changes to your XML documents.I believe this library is complementary to the functions modifying XML elements and attributes available in the FunctX XQuery library. Wouldn't it be cool to have these functions added to FunctX? I leave it to Ryan Grimm and Priscilla Walmsley to discuss this in detail.




