RTF
RTF

Reputation: 6504

Is it possible to use non-recursive CSS selectors with the Ruby Nokogiri gem?

If I have HTML like this:

<div id="target">
    <div id="a1">
        <div class="1">
    </div>
    <div id="a2">
        <div class="2">
        <div class="3">
        <div class="4">
    </div>
    <div id="b1">
        <div class="1">
    </div>
    <div id="b2">
        <div class="2">
        <div class="3">
        <div class="4">
    </div>
    ...
    ...
</div>

...is it possible to select only the first level of div elements from target. So, if I run page.css('#target div').each then I get every single div inside target. If I just want the results to contain the divs with ids ['a1','a2','b1','b2'] is that possible with Nokogiri?

Upvotes: 0

Views: 694

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84134

Yes, this is called a child selector

#target > div

Without the > you have a descendant selector, which doesn't care if it's a child, grandchild, great grandchild etc.

Upvotes: 1

Related Questions