Insert data into XML using Basex

I store two XML documents, namely: a hospital and an office, in BaseX.

The following is the xml office:

<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 to add items using BaseX?

+5
source share
1 answer

XQuery has an updater, an official W3C recommendation called XQuery Update, to restructure the document.

You can use these updates:

Given that you created the database staff, with the message:

CREATE DB office /path/to/office.xml

Now you can use the XQuery Update tool 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 be the node link referenced $upat the last position in your databasestaff

BaseX :

XQuery Update , xmlmind.com.

, API , , .

, , ; , BaseX.

+9

All Articles