user707779
user707779

Reputation:

XPath - select parent nodes based on multiple child selection

Let we have XML looks like that

<bookshelf>
  <cathegory name = "Programming" />
  <book name = "Tille 1" >
    <author>....</author>
  </book>
  <book name = "Tille 2" >
    <author>....</author>
    <translator></translator>
  </book>
  <book name = "Tille 3" >
    <author>....</author>
    <translator>John D.</translator> <!-- non-empty nodes are acceptred! -->
  </book>
</bookshelf>

How can we select bookshelves which have cathegory node with name attribute and at least one book with non-empty translator node?

Basic XPath tutorials do not provide so complicated examples.

Upvotes: 4

Views: 5484

Answers (1)

choroba
choroba

Reputation: 241848

You can chain the conditions one after another:

//bookshelf[cathegory/@name][.//translator/text()]

It selects a bookshelf for which there is a cathegory child with a name attribute, and which hash a non-empty translator descendant.

Upvotes: 9

Related Questions