David Rz Ayala
David Rz Ayala

Reputation: 2235

How to find sibling group with Nokogiri

For example I have this XML:

<root>
  <group>
    <person gender="male" name="Daniel" />
  </group>
  <group>
    <person gender="male" name="Peter" />
    <person gender="female" name="Claudia" />
  </group>
  <group>
    <person gender="female" name="Andrea" />
  </group>
</root>

I want to find only groups that have a male and a female person. I just want to find:

  <group>
    <person gender="male" name="Peter" />
    <person gender="female" name="Claudia" />
  </group>

Because inside that group there is a male and a female.

I don't want to find:

  <group>
    <person gender="female" name="Andrea" />
  </group>
  <group>
    <person gender="male" name="Daniel" />
  </group>

Upvotes: 0

Views: 187

Answers (1)

james31rock
james31rock

Reputation: 2705

I'm not entirely familiar with Nokogiri, but I do know xpath. If you want to select the group with male and females only you can do this

//group[person/@gender='male' and person/@gender = 'female']

It should return

<group> 
  <person gender="male" name="Peter"/>  
  <person gender="female" name="Claudia"/> 
</group>

Upvotes: 2

Related Questions