简体   繁体   中英

Use Nokogiri to get all nodes in an element that contain a specific attribute name

I'd like to use Nokogiri to extract all nodes in an element that contain a specific attribute name.

eg, I'd like to find the 2 nodes that contain the attribute "blah" in the document below.

@doc = Nokogiri::HTML::DocumentFragment.parse <<-EOHTML
<body>
  <h1 blah="afadf">Three's Company</h1>
  <div>A love triangle.</div>
   <b blah="adfadf">test test test</b>
</body>
EOHTML

I found this suggestion (below) at this website: http://snippets.dzone.com/posts/show/7994 , but it doesn't return the 2 nodes in the example above. It returns an empty array.

# get elements with attribute:
elements = @doc.xpath("//*[@*[blah]]")

Thoughts on how to do this?

Thanks! I found this here

elements = @doc.xpath("//*[@*[blah]]")

This is not a useful XPath expression. It says to give you all elements that have attributes that have child elements named 'blah'. And since attributes can't have child elements, this XPath will never return anything.

The DZone snippet is confusing in that when they say

elements = @doc.xpath("//*[@*[attribute_name]]")

the inner square brackets are not literal... they're there to indicate that you put in the attribute name. Whereas the outer square brackets are literal. :-p

They also have an extra * in there, after the @ .

What you want is

elements = @doc.xpath("//*[@blah]")

This will give you all the elements that have an attribute named 'blah'.

您可以使用CSS选择器:

elements = @doc.css "[blah]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM