Stormdamage
Stormdamage

Reputation: 389

XQuery - Remove empty nodes using eXist-db

I am trying to remove all empty <paragraph> nodes in my XML. Here's what I have:

<root>
    <text>
        <paragraph>This is some text</paragraph>
        <paragraph></paragraph>
        <paragraph>More text</paragraph>
    </text>
    <text>
        <paragraph>Another paragraph</paragraph>
        <paragraph></paragraph>
    </text>
</root>

And here is my XQuery:

let $root-ele as element() := doc("text.xml")/*
return 
    update delete $root-ele/text[paragraph = '']

It doesn't seem to be updating the file, but I'm not sure why?

Upvotes: 0

Views: 1077

Answers (1)

Joe Wicentowski
Joe Wicentowski

Reputation: 5294

Your code, as written, should delete both <text> elements - not the empty <paragraph> elements. Let's set that issue aside for now and figure out why your code isn't having any effect on the source file.

First, are you sure it isn't having any effect? Depending on your editing environment, you likely need to close and reopen the text.xml document before seeing changes.

Once you're sure the document is really not being modified, I would guess that your doc() function is not properly selecting the document correctly. To troubleshoot, try supplying the full, absolute path to your document, e.g., doc('/db/text.xml') or doc('/db/apps/my-app/text.xml').

Once you confirm that you're correctly addressing the file, use an XPath to confirm that you are selecting the desired nodes, e.g., doc('/db/test.xml')/root/text/paragraph[. = ''] to select empty paragraphs.

Once you are able to see the desired nodes, I would suggest using a FLWOR to delete each instance in question:

for $p in doc('/db/test.xml')/root/text/paragraph[. = '']
return
    update delete $p

As the documentation about eXist's XQuery Update syntax makes clear - see paragraph 3 of http://exist-db.org/exist/apps/doc/update_ext.xml#syntax - you should always try to make your update as close to the target node of the update as possible.

... be cautious when deleting or replacing nodes that are still being used by enclosing code. This is because a delete or replace will be processed immediately, and the deleted or replaced node will no longer be available to the query... However, an expression like the following [see sample code in link] is safe as it only modifies the current iteration variable...

Upvotes: 5

Related Questions