Insertion of an data in an XML using Basex

我的梦境 提交于 2020-01-02 03:40:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9407743/insertion-of-an-data-in-an-xml-using-basex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!