Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn Nokogiri XML object to string without using .text

I have an object which is a child of a < td > tag. The class is ctext and the relevant data is within the < b > tag.tag

I have selected the node using:

td.css(".ctext b")

and it seems to work. I get a result of something like:

< b >Flying< br >< br >At the beginning of each combat, choose first strike, vigilance, or lifelink. Creatures you control gain that ability until end of turn.< /b >

If I use:

td.css(".ctext b").text

to convert it to a string, I get:

FlyingAt the beginning of each combat, choose first strike, vigilance, or lifelink. Creatures you control gain that ability until end of turn.

What I need is to be able to convert the td.css(".ctext b"), which I think is a Nokogiri xml node to string without stripping the HTML tags. I need to keep the < br >s.

like image 551
Aron Greenspan Avatar asked Apr 09 '13 01:04

Aron Greenspan


2 Answers

You want .inner_html instead of .text.

like image 179
Mark Thomas Avatar answered Nov 14 '22 19:11

Mark Thomas


The to_s method on Nokogiri::XML::Node should return the full HTML or XML representation, including tags:

to_s()
Turn this node in to a string. If the document is HTML, this method returns html. If the document is XML, this method returns XML.

So I would try this:

td.css(".ctext b").to_s
like image 26
Stuart M Avatar answered Nov 14 '22 18:11

Stuart M