我试图在 html 片段中找到单词以用 href 替换它们。不知何故找不到用于 Xpath 的正确路径。例:
require 'nokogiri'
html = '
<p>A paragraph Apple<p>
<span>Apple</span>
<ul>
<li>Item 1</li>
<li>Apple <strong>Apple</strong></li>
<li>Apple</li>
<li>Orange</li>
</ul>
<p><i>Apple</i>Apple</p>'
doc = Nokogiri::HTML.fragment(html)
doc.xpath('.//*[text()="Apple"]').each do |node|
puts "n"
puts node.name
puts node.content
puts node.replace('REPLACED')
end
puts doc.to_html
结果:
span
Apple
REPLACED
strong
Apple
REPLACED
li
Apple
REPLACED
i
Apple
REPLACED
<p>A paragraph Apple</p><p>
REPLACED
</p><ul>
<li>Item 1</li>
<li>Apple REPLACED</li>
REPLACED
<li>Orange</li>
</ul>
<p>REPLACEDApple</p>
因此,根 p 元素中的单词不会被替换,而只剩下一个 li 中的单词。在这种情况下,我应该使用哪个路径来搜索 root 和所有子项?在这样的页面上阅读 .//* 应该是用于在根节点和子节点中进行搜索的路径。关于如何使用nokogiri或xpath正确处理此问题的任何想法?
提前感谢!
您正在寻找整个文本等于 "Apple"
的节点,而不是包含"Apple"
的节点
html = '
<p>A paragraph Apple<p>
<span>Apple</span>
<ul>
<li>Item 1</li>
<li>Apple <strong>Apple</strong></li>
<li>Apple</li>
<li>Orange</li>
</ul>
<p><i>Apple</i>Apple</p>
<Apple>Dont replace!</Apple>
'
doc = Nokogiri::HTML.fragment(html)
doc.traverse do |node|
if node.text?
node.content = node.content.gsub('Apple', 'REPLACED')
end
end
puts doc.to_html
它输出:
<p>A paragraph REPLACED</p><p>
<span>REPLACED</span>
</p><ul>
<li>Item 1</li>
<li>REPLACED <strong>REPLACED</strong>
</li>
<li>REPLACED</li>
<li>Orange</li>
</ul>
<p><i>REPLACED</i>REPLACED</p>
<apple>Dont replace!</apple>