Aron Greenspan
Aron Greenspan

Reputation: 115

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.

Upvotes: 10

Views: 10130

Answers (2)

Mark Thomas
Mark Thomas

Reputation: 37517

You want .inner_html instead of .text.

Upvotes: 9

Stuart M
Stuart M

Reputation: 11588

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

Upvotes: 7

Related Questions