I'm working on a vim rspec plugin (https://github.com/skwp/vim-rspec) - and I am parsing some html from rspec. It looks like this:
doc = %{
<dl>
<dt id="example_group_1">This is the heading text</dt>
Some puts output here
</dl>
}
I can get the entire inner of the using:
(Hpricot.parse(doc)/:dl).first.inner_html
I can get just the dt by using
(Hpricot.parse(doc)/:dl).first/:dt
But how can I access the "Some puts output here" area? If I use inner_html, there is way too much other junk to parse through. I've looked through hpricot docs but don't see an easy way to get essentially the inner text of an html element, disregarding its html children.
Note that this is bad HTML you have there. If you have control over it, you should wrap the content you want in a
<dd>
.In XML terms what you are looking for is the TextNode following the
<dt>
element. In my comment I showed how you can select this node using XPath in Nokogiri.However, if you must use Hpricot, and cannot select text nodes using it, then you could hack this by getting the
inner_html
and then stripping out the unwanted: