Modifying Documents

XQuery as a language does not yet define any way to modify documents stored within an XML database. When it does, BDB XML will add support for this functionality. Meanwhile, BDB XML does include an excellent API for document modification. Using this API it is possible to add new data into an existing document, modify (replace) existing data in the document, and delete data from a document.

In the BDB XML shell, modifications occur in two steps. First, you select the set of documents you want to work with. In this example only the component1 document is selected:

dbxml> query doc('components.dbxml/component1')

1 objects returned for eager expression 'doc('components.dbxml/component1')'

Second, specify the modification to make against the query result. For our example, there are many possible modification operations. For this example, we'll simply add a new uses-part element to the document:

dbxml> append ./component element uses-part '12' 

Appending into nodes: ./component an object of type: element with name: 
uses-part and content: 12

1 modifications made.

dbxml> print
<?xml version="1.0" encoding="UTF-8" standalone="no" ?><component number="1">
<uses-part>89</uses-part>
<uses-part>150</uses-part>
<uses-part>899</uses-part>
<uses-part>12</uses-part></component>

The append command executes relative to the context of the previous query. That context contains the entire component1 document because we used the doc function to select it. However, modifications only work against nodes within the document so we had to select the root node of the document with the ./component XPath statement.

Modifications can be made against the result of any query regardless of the number of documents returned.

dbxml> query '
collection("components.dbxml")/component'

2 objects returned for eager expression '
collection("components.dbxml")/component'

The query selected all the documents in the container. Now, insert a new uses-part element after the last uses-part element in each document.

dbxml> insertAfter ./uses-part[last()] element uses-part '15'

Inserting after nodes: ./uses-part[last()] an object of type: element with 
name: uses-part and content: 15

2 modifications made.

dbxml> print
<component number="1">
<uses-part>89</uses-part>
<uses-part>150</uses-part>
<uses-part>899</uses-part>
<uses-part>12</uses-part><uses-part>15</uses-part></component>
<component number="2">
<uses-part>901</uses-part>
<uses-part>87</uses-part>
<uses-part>189</uses-part><uses-part>15</uses-part>
</component>

The modification becomes part of the current context . This allows for cascading modifications. Taking advantage of this, the following will remove the nodes added in the last step using the removeNodes command.

dbxml> removeNodes 'uses-part[. = 15]'
Removing nodes: uses-part[. = 15]

2 modifications made.

dbxml> print
<component number="1">
<uses-part>89</uses-part>
<uses-part>150</uses-part>
<uses-part>899</uses-part>
<uses-part>12</uses-part></component>
<component number="2">
<uses-part>901</uses-part>
<uses-part>87</uses-part>
<uses-part>189</uses-part>
</component>

Such modifications can also be performed on attributes, processing instructions, comments and text nodes by altering the parameters to the modification operation. The following adds a version attribute to the root node of each component.

dbxml> append . attribute version '1.0'
Appending into nodes: . an object of type: attribute with name: version and 
content: 1.0

2 modifications made.

dbxml> print
<component number="1" version="1.0">
<uses-part>89</uses-part>
<uses-part>150</uses-part>
<uses-part>899</uses-part>
<uses-part>12</uses-part></component>
<component number="2" version="1.0">
<uses-part>901</uses-part>
<uses-part>87</uses-part>
<uses-part>189</uses-part>
</component>

Modification operations provide a powerful and simple mechanism for altering XML data without the overhead of removing that data from the database. Large documents can be quickly and easily manipulated when combining this feature with node level storage (the default).