1

Assume the following tree:

<root>
 <a></a>
 <b>  </b>
 <c>
 </c>
 <d>Hello world</d>
 <e><f>!!!</f></e>
</root>

I need to be able to select all empty tags, so a, b, and c in the above example. In my scenario, I consider b and c as being empty, despite them having white spaces.

Further, I would not consider e as being empty as it has a child.

How do I select all elements that do no have a child, whose text node is completely empty or comprises only of white spaces?

The following XPath would select a, but miss b and c:

`.//*[not(text())`]

My feeling is that I need to use normalize-space() somehow, but I'm not sure how.

0

1 Answer 1

2

Yes, with XPath1 you gonna need normalize-space. Try this:

.//*[not(*)][not(normalize-space(text()))]

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

2 Comments

As it turns out, I do not want to delete all empty tags. Some tags are going to be exempt from this. How would I except, for example, all foo and bar tags?
Add more conditions like .//*[not(*)][not(normalize-space(text()))][not(name()='foo')][not(name()='bar')]

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.