不使用.text将Nokogiri XML对象转换为字符串

时间:2013-04-09 01:53:38

标签: html ruby nokogiri

我有一个< td >标签的子对象。该课程为ctext,相关数据位于< b > tag.tag

我使用以下方法选择了节点:

td.css(".ctext b")

它似乎有效。我得到了类似的结果:

< 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 >

如果我使用:

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

将其转换为字符串,我得到:

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

我需要的是能够将td.css(".ctext b")(我认为是Nokogiri xml节点)转换为字符串而不剥离HTML标记。我需要保留< br > s。

2 个答案:

答案 0 :(得分:5)

to_s上的Nokogiri::XML::Node方法应返回完整的HTML或XML表示形式,包括标记:

  

<强> to_s()
  将此节点转换为字符串。如果文档是HTML,则此方法返回html。如果文档是XML,则此方法返回XML。

所以我会试试这个:

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

答案 1 :(得分:4)

您希望.inner_html代替.text