0

I have a BaseX database with 3 documents in it. The third one is generated by operating on the first two. I need to do some additional operations to generate a 4th document and was hoping I could generate a copy of my third document and then modify it.

I know how to use the copy and modify commands, but I don't want to return xml, write to a file and then add that file. I want to duplicate the document without having to save any new files.

Any thoughts?

David K
  • 213
  • 1
  • 2
  • 12
  • well, if you duplicate a document you will have to save a new file by definition (although, of course, internally it will not be a new file but stored in a binary storage format). Also, why do you not want to use the transform expressions? It seems to me the most efficient solution if you don't have a very large document - It does not imply that the complete XML is serialized in any way. It might help if you add your code you have tried and explain why it is not a viable solution for you. – dirkk Mar 24 '15 at 13:32
  • Thanks for the suggestion. I have not tried transform since what I want to do is just make a copy of a document just omitting nodes with specific values and I just thought there would be a simple way to duplicate the source document and then delete nodes. (i.e. in the classic bookstore example, lets just say I wanted to eliminate all books with an author of "John Doe"). Anyways, I will look into your suggestion. I am fairly new to xQuery, and appreciate your help. – David K Mar 24 '15 at 14:52
  • Non-Updating transform expressions *are* a simple way to do that - I can't think of a way to simplify it even more. Take a look at the examples at http://docs.basex.org/wiki/Updates#transform - Instead of just `return $c` simply do a `return db:add("new-document", $c)` – dirkk Mar 24 '15 at 15:06
  • Also, to further expand why transform is better here: If you first copy the source document and then later update it it (i.e. you have to write twice) will be more expansive than simply transforming a document in main memory and later write it. – dirkk Mar 24 '15 at 15:08
  • And as you are also fairly new to SO (Welcome!) one more comment: If you provide a _concrete_ example input here and your expected output we can help you here with a concrete answer. We do not really work with quite unspecific example content here (your "classic bookstore" example can be modeled quite differently) – dirkk Mar 24 '15 at 15:10
  • Thank you dirkk for all your suggestions. I really appreciate the help, it is a big improvement over what I was doing. I will also be sure to provide concrete examples in the future. Thanks for helping even though my question was a mess. All the best – David K Mar 25 '15 at 13:57

1 Answers1

0

The documentation for basex is very poor. However, since I had to do the same thing, here is how I did it.

Given a database named config which contains a document called a; to copy a to b do:

db:add("config", db:open("a"), "b")

It's worth noting that the document name doesn't have to be unique. In our example if b already exists there will be two documents called b.

If b has to be unique, you can just delete it before you create it. basex doesn't appear to throw an error if the target document doesn't exist.

(
    db:delete("config", "b"),
    db:add("config", db:open("a"), "b")
)
shrewmouse
  • 5,338
  • 3
  • 38
  • 43