Reputation: 3121
I have some XML that looks like
<page id="1">
<subpages>
<page id="2">
<subpages>
<page id="3">
</subpages>
</page>
<page id="4"></page>
<page id="5"></page>
</subpages>
</page>
What I'm trying to do is pull out the top level <page>
in the top <subpages>
tag meaning I'm trying to pull out pages 2, 4, and 5, but not 3.
Right now I'm doing //subpages[1]/page
but this gets all the pages inside of the first subpages tag. Is there a way to do this?
I'm able to do this in jQuery with
var c = $(data).find("subpages").first();
$(c).children("page").each(function() {});
If I can't get a proper xpath to work, is there a way to make Nokogiri behave like jQuery does?
Upvotes: 0
Views: 71
Reputation: 37517
The equivalent of your jQuery would be:
doc.at('subpages').xpath('page').each do |page|
puts page.attr('id')
end
Result:
2
4
5
Upvotes: 0
Reputation: 52868
The simplest way would be:
/*/*/page
More explicitly:
/page/subpages/page
Also, you could've done this (but I try to avoid //
):
(//subpages)[1]/page
Upvotes: 1