I am storing two XML documents, namely hospital and office, in BaseX.
The following is the office xml:
<Staff>
    <Employee Name="Brian">
        <Personal>
            <SSN> 666-66-6666 </SSN>
        </Personal>
        <StaffInfo>
            <Position> Doctor </Position>
            <AccountableTo> David </AccountableTo>
        </StaffInfo>
    </Employee>
    <Employee Name="David">
        <Personal>
            <SSN> 555-55-5555 </SSN>
        </Personal>
        <StaffInfo>
            <Position> Doctor </Position>
            <AccountableTo />
        </StaffInfo>
    </Employee>
</Staff>
In this XML I want to add one or more employees. How can I add elements using BaseX?
XQuery has an update facility, an official W3C recommendation, called XQuery Update to change the document structure.
You can use updates like so:
Given you have created a database employees, with the commmand:
CREATE DB office /path/to/office.xml
Now you may use the XQuery Update facility and run the following query:
let $up := <Employee Name="Joe">
    <Personal>
      <SSN>666-66-1234</SSN>
    </Personal>
    <StaffInfo>
      <Position>Doctor</Position>
      <AccountableTo>Jeff</AccountableTo>
    </StaffInfo>
  </Employee>
  return
insert node $up as last into doc('office')/Staff
This will ad the node referenced by $up at the last position in your database staff
The BaseX Documentation Wiki contains more information on updates:
There is a good tutorial, XQuery Update for the impatient provided by xmlmind.com.
Sure enough you can use the APIs to issue these queries, for a start I'd suggest you stick with GUI, so you can see results directly.
Hope this helped, feel free to ask for more information; either here or on the official BaseX Mailing List.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With