Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在文档片段中查找注释或文本节点_Html_Ruby_Xml_Nokogiri - Fatal编程技术网

Html 在文档片段中查找注释或文本节点

Html 在文档片段中查找注释或文本节点,html,ruby,xml,nokogiri,Html,Ruby,Xml,Nokogiri,我必须清理Nokogiri::HTML::DocumentFragment文档(删除仅包含空格的注释节点和文本节点)。下面是一个例子: html = "<p>paragraph</p><!-- comment --><p>paragraph</p> <p>paragraph</p>" doc = Nokogiri::HTML::DocumentFragment.parse html 如何在此文档片段中找到所

我必须清理Nokogiri::HTML::DocumentFragment文档(删除仅包含空格的注释节点和文本节点)。下面是一个例子:

html = "<p>paragraph</p><!-- comment --><p>paragraph</p>   <p>paragraph</p>"
doc = Nokogiri::HTML::DocumentFragment.parse html
如何在此文档片段中找到所有注释或所有文本节点

以下内容无效,因为它不是完整文档,而是文档片段:

doc.search('//text()')
doc.search('//comment()')
我想出来了:

doc.search('.//text()')
doc.search('.//comment()')
doc.search('.//text()')
doc.search('.//comment()')