我正在寻找一些关于如何完成的建议。我正在尝试仅使用 xpath 的解决方案:
一个html示例:
<div>
<div>
<div>text div (leaf)</div>
<p>text paragraph (leaf)</p>
</div>
</div>
<p>text paragraph 2 (leaf)</p>
代码:
doc = Nokogiri::HTML.fragment("- the html above -")
result = doc.xpath("*[not(child::*)]")
[#<Nokogiri::XML::Element:0x3febf50f9328 name="p" children=[#<Nokogiri::XML::Text:0x3febf519b718 "text paragraph 2 (leaf)">]>]
但是这个 xpath 只给了我最后一个“p”。我想要的是一个扁平化的行为,只返回叶子节点。
以下是stackoverflow中的一些参考答案:
谢谢