Friday, April 20, 2012

Excluding elements contained within another element with CSS or XPath

Here's a chunk of XML:

<section class="ignored">

It's easy enough to select all elements, or all elements inside a section.ignored:

=> "goodbad"
@doc.css('section.ignored element').text
=> "bad"

But how do I select all elements that are not inside section.ignored? This doesn't work:

@doc.css('section:not(.ignored) element').text
=> "goodbad"

...because that actually means "all elements that are contained in any section that is not ignored", including the top-level section that wraps everything else!

Additional twist: unlike the simplified sample above, the real XML I have to deal with is nested to arbitrary depth, including sections within the ignored section(s).

And yes, I could just substract the bad array from the full array in Ruby and call it a day, but I'd prefer a pure CSS solution (or, if you must, XPath) if possible.

No comments:

Post a Comment