Reputation: 7227
How can I search by content of a node using nokogiri::XML ?
Lets say I have the following xml
<parts>
<part>
<name>foo</name>
<madein>
<city>ABC</city>
<country>XYZ</country>
</madein>
</part>
<part>
<name>foo</name>
<madein>
<city>PQR</city>
<country>XYZ</country>
</madein>
</part>
<part>
<name>foo</name>
<madein>
<city>ABC</city>
<country>XYZ</country>
</madein>
</part>
</parts>
And I want to get all parts for which /madein/city is ABC. What is the best way to get the parts nodes ?
I am using nokogiri gem.
Thanks
Upvotes: 1
Views: 114
Reputation: 27207
Xpath is query language for XML, and is very flexible.
To get you started:
doc = Nokogiri::XML::Document.parse( xml_string )
parts_from_abc = doc.xpath( '/parts/part[madein/city="ABC"]' )
As bdares suggested in the comments, if you want to do more, take a look at the tutorials.
Upvotes: 2