将XPath与HTML或XML片段一起使用?

dba*_*osa 1 ruby xpath nokogiri

我是Nokogiri和XPath的新手,我试图访问HTML或XML片段中的所有注释.我没有使用该函数时,XPath .//comment()//comment()工作fragment,但他们找不到任何片段.使用标记而不是注释,它适用于第一个XPath.

通过反复试验,我意识到在这种情况下comment()只能找到顶级评论,.//comment()而其他一些评论只能找到内部评论.难道我做错了什么?我错过了什么?任何人都可以解释发生了什么?

我应该使用什么XPath来获取Nokogiri解析的HTML片段中的所有注释?

这个例子可以帮助理解问题:

str = "<!-- one --><p><!-- two --></p>"

# this works:
Nokogiri::HTML(str).xpath("//comment()")
=> [#<Nokogiri::XML::Comment:0x3f8535d71d5c " one ">, #<Nokogiri::XML::Comment:0x3f8535d71cf8 " two ">]
Nokogiri::HTML(str).xpath(".//comment()")
=> [#<Nokogiri::XML::Comment:0x3f8535cc7974 " one ">, #<Nokogiri::XML::Comment:0x3f8535cc7884 " two ">]

# with fragment, it does not work:
Nokogiri::HTML.fragment(str).xpath("//comment()")
=> []
Nokogiri::HTML.fragment(str).xpath("comment()")
=> [#<Nokogiri::XML::Comment:0x3f8535d681a8 " one ">]
Nokogiri::HTML.fragment(str).xpath(".//comment()")
=> [#<Nokogiri::XML::Comment:0x3f8535d624d8 " two ">]
Nokogiri::HTML.fragment(str).xpath("*//comment()")
=> [#<Nokogiri::XML::Comment:0x3f8535d5cb8c " two ">]
Nokogiri::HTML.fragment(str).xpath("*/comment()")
=> [#<Nokogiri::XML::Comment:0x3f8535d4e104 " two ">]

# however it does if it is a tag instead of a comment:
str = "<a desc='one'/> <p><a>two</a><a desc='three'/></p>"
Nokogiri::HTML.fragment(str).xpath(".//a")
=> [#<Nokogiri::XML::Element:0x3f8535cb44c8 name="a" attributes=[#<Nokogiri::XML::Attr:0x3f8535cb4194 name="desc" value="one">]>, #<Nokogiri::XML::Element:0x3f8535cb4220 name="a" children=[#<Nokogiri::XML::Text:0x3f8535cb3ba4 "two">]>, #<Nokogiri::XML::Element:0x3f8535cb3a3c name="a" attributes=[#<Nokogiri::XML::Attr:0x3f8535cb3960 name="desc" value="three">]>]
Run Code Online (Sandbox Code Playgroud)

PS:没有fragment它做我想要的,但它也添加了一些像"DOCTYPE"的东西,我真的只有一个我正在编辑的HTML文件的片段(删除一些标签,替换其他标签).

Den*_*fel 7

//comment() 是一种简短的形式 /descendant-or-self::node()/child::comment()

将此xpath与片段一起使用会忽略根注释(它们被选中,/descendant-or-self::node()但它们没有子项).

如果您使用HTML(str),则创建一个文档节点作为所有其他项的根.因此,/descendant-or-self::node()/child::comment()不会忽略顶级注释,因为它们是文档节点的子节点(它本身是由它选择的/descendant-or-self::node()).

我不知道为什么descendant::comment()在任何情况下都有效,我会说它应该是descendant-or-self::comment(),但没关系.

希望有帮助吗?