如何在创建`Nokogiri :: XML`或`Nokogiri :: HTML`对象时避免创建非重要的空白文本节点

Cod*_*gMo 11 html-parsing nokogiri xml-parsing

在解析缩进的XML时,从闭合和开始标记之间的空白区域创建非重要的空白文本节点.例如,从以下XML:

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
Run Code Online (Sandbox Code Playgroud)

其字符串表示如下,

 "<note>\n  <to>Tove</to>\n  <from>Jani</from>\n  <heading>Reminder</heading>\n  <body>Don't forget me this weekend!</body>\n</note>\n"
Run Code Online (Sandbox Code Playgroud)

Document创建以下内容:

#(Document:0x3fc07e4540d8 {
  name = "document",
  children = [
    #(Element:0x3fc07ec8629c {
      name = "note",
      children = [
        #(Text "\n  "),
        #(Element:0x3fc07ec8089c {
          name = "to",
          children = [ #(Text "Tove")]
          }),
        #(Text "\n  "),
        #(Element:0x3fc07e8d8064 {
          name = "from",
          children = [ #(Text "Jani")]
          }),
        #(Text "\n  "),
        #(Element:0x3fc07e8d588c {
          name = "heading",
          children = [ #(Text "Reminder")]
          }),
        #(Text "\n  "),
        #(Element:0x3fc07e8cf590 {
          name = "body",
          children = [ #(Text "Don't forget me this weekend!")]
          }),
        #(Text "\n")]
      })]
  })
Run Code Online (Sandbox Code Playgroud)

这里有很多类型的空白节点Nokogiri::XML::Text.


我想计算childrenNokogiri XML中每个节点的数量Document,并访问第一个或最后一个子节点,不包括非重要的空格.我希望不要对其进行解析,或那些和显著文本节点,如那些元素中区分<to>,像"Tove".这是我正在寻找的rspec:

require 'nokogiri'
require_relative 'spec_helper'

xml_text = <<XML
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
XML

xml = Nokogiri::XML(xml_text)

def significant_nodes(node)
  return 0
end

describe "Stackoverflow Question" do
  it "should return the number of significant nodes in nokogiri." do
    expect(significant_nodes(xml.css('note'))).to eq 4
  end
end
Run Code Online (Sandbox Code Playgroud)

我想知道如何创建该significant_nodes功能.

如果我将XML更改为:

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
  <footer></footer>
</note>
Run Code Online (Sandbox Code Playgroud)

然后当我创建时Document,我仍然希望页脚代表; 使用config.noblanks不是一种选择.

tor*_*o2k 15

您可以使用该NOBLANKS选项来解析XML字符串,请考虑以下示例:

require 'nokogiri'

string = "<foo>\n  <bar>bar</bar>\n</foo>"
puts string
# <foo>
#   <bar>bar</bar>
# </foo>

document_with_blanks = Nokogiri::XML.parse(s)

document_without_blanks = Nokogiri::XML.parse(s) do |config|
  config.noblanks
end

document_with_blanks.root.children.each { |child| p child }
#<Nokogiri::XML::Text:0x3ffa4e153dac "\n  ">
#<Nokogiri::XML::Element:0x3fdce3f78488 name="bar" children=[#<Nokogiri::XML::Text:0x3fdce3f781f4 "bar">]>
#<Nokogiri::XML::Text:0x3ffa4e15335c "\n">

document_without_blanks.root.children.each { |child| p child }
#<Nokogiri::XML::Element:0x3f81bef42034 name="bar" children=[#<Nokogiri::XML::Text:0x3f81bef43ee8 "bar">]>
Run Code Online (Sandbox Code Playgroud)

NOBLANKS不该删除空节点:

doc = Nokogiri.XML('<foo><bar></bar></foo>') do |config|
  config.noblanks
end

doc.root.children.each { |child| p child }
#<Nokogiri::XML::Element:0x3fad0fafbfa8 name="bar">
Run Code Online (Sandbox Code Playgroud)

OP指出Nokogiri网站(以及libxml网站上)关于解析器选项的文档非常含糊,遵循NOBLANKS选项的行为规范:

require 'rspec/autorun'
require 'nokogiri'

def parse_xml(xml_string)
  Nokogiri.XML(xml_string) { |config| config.noblanks }
end

describe "Nokogiri NOBLANKS parser option" do

  it "removes whitespace nodes if they have siblings" do
    doc = parse_xml("<root>\n <child></child></root>")
    expect(doc.root.children.size).to eq(1)
    expect(doc.root.children.first).to be_kind_of(Nokogiri::XML::Node)
  end

  it "doesn't remove whitespaces nodes if they have no siblings" do
    doc = parse_xml("<root>\n </root>")
    expect(doc.root.children.size).to eq(1)
    expect(doc.root.children.first).to be_kind_of(Nokogiri::XML::Text)
  end

  it "doesn't remove empty nodes" do
    doc = parse_xml('<root><child></child></root>')
    expect(doc.root.children.size).to eq(1)
    expect(doc.root.children.first).to be_kind_of(Nokogiri::XML::Node)
  end

end
Run Code Online (Sandbox Code Playgroud)