mrk2
mrk2

Reputation: 817

Count parents by child with specific attribute

I have following XML file:

<parent>
  <child status = 'pass'></child>
  <child status = 'pass'></child>
</parent>
  <child status = 'pass'></child>
  <child status = 'fail'></child>
<parent>
  <child status = 'pass'></child>
  <child status = 'pass'></child>
</parent>
<parent>
  <child>
    <child status = 'pass'></child>
    <child status = 'fail'></child>
  </child>
</parent>

I need to count parents by child's status. For example if all children has 'pass' status, the parent's status has to be 'pass', but if one of the children has fail status, the overall status has to be 'fail'. How can I do this by XPath?

Note that some parent can contain child with its own children, so the XPath has to be versatile. Any ideas?

Upvotes: 1

Views: 199

Answers (1)

kjhughes
kjhughes

Reputation: 111551

First, wrap your parent elements in a common root element, else the XML is not well formed.

Then you can use the following XPath to count all of the parent elements for which no descendant child element has a fail @status:

count(//parent[not(.//child/@status = 'fail')])

Upvotes: 1

Related Questions