0

I have a XML file with multiple level structure (it is a directory structure with files, thus there are <directory name="..."> and <file name="..."> elements)

and I want to select empty directories with XPath, but I don't know, how to select nodes, which doesn't have some descendants. I can match the ns:directory[not(ns:file)] but it is only the first level of emtpy directories and I dont want to do anything like ns:directory[not(ns:file)]|ns:directory[ns:directory[not(ns:file)]]|...

Could someone please hint, how to do this?

1
  • If you search for "descendant" in the xpath spec you'll see that there is a descendant:: axis that will help you here. Commented Feb 18, 2014 at 10:17

2 Answers 2

1

Easy.

//ns:directory[not(.//ns:file)] 
Sign up to request clarification or add additional context in comments.

Comments

0

The answer above will exclude directories that only contain directories. If that's what you want, fine. If you're looking for directories that are completely empty, try this:

//ns:directory[not(ns:file|ns:directory)] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.