remove_attribute 返回 nil 节点元素而不是仅仅删除属性 nokogiri

remove_attribute returning nil node element instead of just deleting attribute nokogiri

我正在尝试从 Nokogiri 节点元素中删除一个属性。

这是第一个节点元素:

=> #(Element:0x3fed0eaf2ef0 {
  name = "ins",
  namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }),
  attributes = [
    #(Attr:0x3fed0eb1e528 { name = "id", namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }), value = "0" }),
    #(Attr:0x3fed0eb1e514 { name = "author", namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }), value = "Mitchell Gould" })
    #(Attr:0x3fed0eb1e500 { name = "date", namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }), value = "2016-10-15T14:43:00Z" })]
  })

当我这样做时:

   first = all_ins_nodes.first.remove_attribute("name")

first => nil

我只想删除属性而不是删除整个节点元素。我该怎么做?

就在这里

first = all_ins_nodes.first.remove_attribute("name")

应该分成多行

first = all_ins_node.first
first.remove_attribute("name")

原因是.remove_attribute显然returns无。您希望指针指向 .first 调用的结果。