I have a following HTML and I want to fetch the parents in the document. I used Nokogiri for parsing:
j_text = "<p>
<a>abc</a>
<a>pqr></a>
</p>
<table>
<tr>
<td><p>example</p></td>
<td><p>find</p></td>
<td><p>by</p></td>
<td><p>ID</p></td>
</tr>
</table>
<p><p>zzzz</p>nnnnn</p>
<u>sfds<u>"
I did:
doc = Nokogiri::HTML(j_text)
Now I want the parent element from above HTML text i.e <p>, <table>, <p>, <u>
using Nokogiri, how do I do this?
doc.xpath('//p[@class="parent"]/text()')
– Epiphragmdoc.css('p').children.remove
then will removechildren
and will give you parent node – Epiphragmdoc.xpath('//p')
– Epiphragmdoc = Nokogiri::HTML::DocumentFragment.parse('j_text')
doc.at('p')
– Epiphragm