2

I need to be able to delete all nodes with any name as long as (a) the node has no attributes or (b) the node has no inner text.

Sample XML:

<mydoc> <delete_me /> <keep_me value="yes!" /> <but_do_not_keep_me></but_do_not_keep_me> <should_you_keep_me>Absolutely!</should_you_keep_me> </mydoc>

The expected output is

<mydoc> <keep_me value="yes!" /> <should_you_keep_me>Absolutely!</should_you_keep_me> </mydoc>

I've got the queries figured out to satisfy one condition or the other.

@xml.modify('delete //*[empty(@*)]')

This will only keep elements with an attribute. Similarly,

@xml.modify('delete //*[empty(node())]')

will remove all empty nodes. But I cannot figure out how to accomplish both. If I try to do a union |, that gives me the error XQuery [modify()]: The XQuery syntax 'union' is not supported..

Any suggestions?

2
  • 3
    So 'delete //*[empty(node()) and empty(@*)]' ? Commented Aug 15, 2017 at 1:16
  • 1
    @ZLK You are a genius. I've been using the | for "and". I had no idea you could use the actual text and to accomplish the same thing. Thank you! Commented Aug 15, 2017 at 1:22

1 Answer 1

1

As per @ZLK's comment above, the and keyword is allowed.

@xml.modify('delete //*[empty(node()) and empty(@*)]')

works perfectly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.