How to get the parent class name of the <p> tag using Nokogiri?

Am I trying to get the parent class name of the <p> ?

 <div class="entry-content"> <p>Some text...</p> </div> 

How can i get this?

+4
source share
4 answers

Some people find using css and the nokogiri parent method easier to read / maintain than xpath:

 html = %q{ <div class="entry-content"> <p>Some text...</p> </div> } doc = Nokogiri::HTML(html) doc.css('p').each do |p| puts p.parent.attr('class') end 
+4
source

Use XPath as //p/.. or //*[p] (the parent of any p element at any depth).

 str =<<__HERE__ <div class="entry-content"> <p>Some text...</p> </div> __HERE__ html = Nokogiri::HTML(str) p_parents = html.xpath('//p/..') # => NodeSet containing the "<div>" element. p_parents.each do |node| puts node.attr('class') # => "entry-content" end 
+3
source

I would use #at_css instead of css .

 require 'nokogiri' str =<<__HERE__ <div class="entry-content"> <p>Some text...</p> </div> __HERE__ html = Nokogiri::HTML(str) p_parent = html.at_css('p').parent p_parent.name # => "div" p_parent['class'] # => "entry-content" 
+2
source

This is a good precedent for XPath. Here's how I do it:

 require 'nokogiri' doc = Nokogiri::HTML(<<EOT) <div class="entry-content"> <p>Some text...</p> </div> EOT puts doc.at('//p/..')['class'] 

What are the outputs: entry-content .

If you can have multiple <p> tags and need to access your parents class, use:

 puts doc.search('//p/..').map{ |n| n['class'] } 

which again displays: entry-content .

In any case, using the [] notation is a shortcut to retrieve the value associated with the tag parameter.

And, as we will see .. on the * nix command line when listing directories, .. means the parent element.

Nokogiri supports the use of CSS selectors to navigate a document, but CSS does not support a parent accessory for a long time. CSS 4 has the ability to get there, but Nokogiri v1.6.0 doesn't seem to support it yet. For example, we can use a selector like $* > p , but it does not work:

 doc.at('$* > p') Nokogiri::CSS::SyntaxError: unexpected '$' after '' doc.at('* > p') => #<Nokogiri::XML::Element:0x3ff7c099f528 name="p" children=[#<Nokogiri::XML::Text:0x3ff7c099f2e4 "Some text...">]> 

$ is a CSS marker that says that a certain part of the selector is what interests us. See " Defining a Selector Theme " for more information. After Nokogiri supports themes, we can optimize our CSS selectors and their accompanying Ruby code, because we will not need to use parent methods to set the parent nodes. Until then, well, we still have old work around using parent .

+1
source

Source: https://habr.com/ru/post/1497839/


All Articles