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

时间:2016-10-26 03:46:19

标签: ruby 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

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

1 个答案:

答案 0 :(得分:1)

就在这里

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

应该分成多行

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

原因是.remove_attribute显然会返回nil。您希望指针指向.first调用的结果。