【问题标题】:Nokogiri: Ignoring child nodesNokogiri:忽略子节点
【发布时间】:2014-07-21 02:02:01
【问题描述】:

我有一个 xml 文档,如下所示:

<doc>
  <header>
    <group>
      <note>group note</note>
    </group>
    <note>header note</note>
  </header>
</doc>

我想检索标题下的笔记元素,而不是组下的任何笔记元素。

我认为这应该可以,但它也会在 group 下找到注释:

 doc.css('header note')

只抓取作为标题的直接子元素的 note 元素的语法是什么?

【问题讨论】:

  • 我假设第二个“”标签应该是一个结束标签 - “”?

标签: ruby nokogiri


【解决方案1】:

您可以在 CSS 选择器中使用 &gt; 来查找子元素。这与使用空格 形成对比,后者查找后代元素。

在你的情况下:

puts doc.css('header > note')
#=> "<note>header note</note>"

【讨论】:

    【解决方案2】:

    最简单的就是让Nokogiri找到所有header note标签,然后只用最后一个:

    require 'nokogiri'
    
    doc = Nokogiri::XML(<<EOT)
    <doc>
      <header>
        <group>
          <note>group note</note>
        <group>
        <note>header note</note>
      </header>
    </doc>
    EOT
    
    doc.css('header note').last.text # => "header note"
    

    请记住,css,就像它的 XPath 对应物 xpath,以及更通用的 search,返回 NodeSet。 NodeSet 就像一个数组,您可以对其进行切片或使用firstlast

    请注意,您也可以轻松使用:

    doc.css('note').last.text # => "header note"
    

    请注意,您的 XML 格式不正确。 &lt;group&gt; 标签未关闭。 Nokogiri 正在对 XML 进行修复,这可能会给您带来奇怪的结果。通过查看doc.errors 来检查这种情况:

    # => [#<Nokogiri::XML::SyntaxError: Opening and ending tag mismatch: group line 5 and header>,
    #     #<Nokogiri::XML::SyntaxError: Opening and ending tag mismatch: group line 3 and doc>,
    #     #<Nokogiri::XML::SyntaxError: Premature end of data in tag header line 2>,
    #     #<Nokogiri::XML::SyntaxError: Premature end of data in tag doc line 1>]
    

    【讨论】: