0
votes

Unfortunately, because of a bug in our code, some of the documents were created with more than one root element with duplicate content

For ex.

fn:doc("doc1.xml") 

would return

<doc1>
    <a>AA</a> 
    </b>
</doc1>
<doc1>
   <a>AA</a>
   </b>
</doc1>

Is there an efficient way to identify all the documents which has more than one root element?

Thanks in advance.

2
I cannot reproduce, get a XDMP-MULTIELEMCHILDREN error. I'd be curious to learn how you managed to get to this situation. Apart from that, a blunt doc()[count(doc1) > 1]/base-uri() should show you the uris. Might take a bit longer to run though.. - grtjn
Thanks for your suggestion. After taking a look at it further, it appears that there may be two documents with same uri. - gjwilson21
is it possible to have more than one document with the same uri in a database? - gjwilson21

2 Answers

1
votes

MarkLogic normally doesn't allow creating documents with multiple root elements, though I am not sure it is guaranteed it can't. There are however known circumstances in which you can end up with multiple documents having identical database uris.

One way is to create two forests for one database, disconnect the second, insert a document, replace first with second forest, then insert same doc again. After that, connect both forests, and you will have the same doc twice at the same database uri, but in a different forest. This shouldn't be possible with just one forest.

How to get rid of these? Loop over doc()[count(doc1) > 1]/base-uri(), and then reinsert those documents. If you pull the value from the database, you will have to decide which doc node is to be preserved, and that might not be easy. You might also want to check how many docs are involved in this situation. You will want to batch up the reinsert if it is over (a few) thousand documents..

HTH!

0
votes

Possible? Maybe. Marklogic does a lot of checks for this so that you don't have this happen. Are both documents in the same forest?