V-X
V-X

Reputation: 3029

XPath match elements, which doesn't contain specified descendant

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?

Upvotes: 0

Views: 96

Answers (2)

rumplestiltzkin
rumplestiltzkin

Reputation: 43

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)]

Upvotes: 0

Tomalak
Tomalak

Reputation: 338208

Easy.

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

Upvotes: 1

Related Questions