繁体   English   中英

XPath:在文本节点中查找节点

[英]XPath: Find a node within a text node

我有以下 html:

<code>The first code block</code>
<p>Some text and <code>the second code block</code> followed by other text</p>

我需要从中查找并删除所有code块。 我使用以下 XPath '//code' ,但它只找到第一个代码块,而第二个仍然存在。

问题:为什么'//code'无法捕获第二个代码块? 如何解决?

详细信息:我正在使用Nokagiry在 Ruby 中进行。 我的代码如下所示:

html = Nokogiri::HTML(File.read(htmlFile))
html.search('//code').remove

更新:

XPath 实际上有效。 我只是在不同的地方犯了一个错误。

好像你忘记了迭代器......
尝试:

html = Nokogiri::HTML(File.read(htmlFile))
html.search('//code').each{|htm| htm.remove}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM