简体   繁体   English

Nokogiri :: XML不会删除节点

[英]Nokogiri::XML doesn't removes node

Actually i have that file: 其实我有那个文件:

<info version="5.0" xml:id="info-Documentation-Test09-Test09" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
        <title>Test09</title>
        <subtitle>Enter a short description here.</subtitle>
        <productname>Documentation</productname>
        <productnumber>0.1</productnumber>
        <edition>0</edition>
        <abstract>
                <para>
                        A short overview and summary of the book's subject and purpose, traditionally no more than one paragraph long. Note: the abstract will appear in the front matter of your book and will also be placed in the description field of the book's RPM spec file.
                </para>
        </abstract>
        <orgname>
        <inlinemediaobject>
                <imageobject>
                        <imagedata fileref="Common_Content/images/title_logo.svg" format="SVG" />
                </imageobject>
        </inlinemediaobject>
        </orgname>
        <xi:include href="Common_Content/Legal_Notice.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
        <xi:include href="Author_Group.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
</info>

Now i would like to remove the node with it's childs. 现在,我想删除带有其子节点的节点。 I have tried out: 我已经尝试了:

def self.remove_orgname(artinfo)
    # Entferne $titelbild des Artikels
    puts 'Entferne Logo aus dem Article_Info File. Wird anders gesetzt.'
    io = File.open("#{artinfo}")
    doc = Nokogiri::XML(io)
    doc.search('//orgname').each do |node|
      node.delete
      node.content = 'Children removed'
    end
    io.close
  end

I'm running the method with giving the File "artinfo = "#{titel}/de-DE/Article_Info.xml". After running the program i can see no errors given from ruby. But it looks like Nokogiri hasn't removed that content. Any ideas? 我正在通过给文件“ artinfo =“#{titel} /de-DE/Article_Info.xml”运行该方法。运行该程序后,我看不到ruby给出的任何错误。但是看来Nokogiri尚未移除该内容,有什么想法吗?

Tried out also: 还尝试了:

def self.remove_orgname(artinfo)
    # Entferne $titelbild des Artikels
    puts 'Entferne Logo aus dem Article_Info File. Wird anders gesetzt.'
    io = File.open("#{artinfo}")
    doc = Nokogiri::XML.fragment(io)
    doc.search('//orgname').each do |node|
      node.delete
      node.content = 'Children removed'
    end
    File.open("#{artinfo}", 'w')
    io.close
  end

But this produces an empty file... 但这会产生一个空文件...

This one doesn't produce a empty file but it looks like it doesn't made a change: 这个不会产生一个空文件,但是看起来它没有做任何改变:

def self.remove_orgname(artinfo)
    # Entferne $titelbild des Artikels
    puts 'Entferne Logo aus dem Article_Info File. Wird anders gesetzt.'
    doc = Nokogiri::XML(IO.read(artinfo))
    doc.search('//orgname').each do |node|
      node.delete
      node.content = 'Children removed'
    end
    IO.write(artinfo, doc.to_xml)
  end

Now i have the solution: 现在我有解决方案:

def self.remove_orgname(artinfo)
    # Entferne $titelbild des Artikels
    puts 'Entferne Logo aus dem Article_Info File. Wird anders gesetzt.'
    doc = Nokogiri::XML(IO.read(artinfo))
    doc.search('orgname').each do |node|
      node.remove 
      node.content = 'Children removed'
    end
    IO.write(artinfo, doc.to_xml)
  end

So it works :-) 这样就可以了:-)

You loaded the file into a Nokogiri document, changed the nokogiri document, didn't save it back to the file and didn't return it out of the function. 您已将文件加载到Nokogiri文档中,更改了nokogiri文档,没有将其保存回文件,也没有将其从函数中返回。 It's like loading a text in Word, changing it, then powering off your computer - you did change things, but without saving it was in vain. 这就像在Word中加载文本,更改文本,然后关闭计算机电源一样-您确实做了更改,但没有保存就徒劳无功。

EDIT: Try this: 编辑:试试这个:

doc = Nokogiri::XML(IO.read(filename))
...
IO.write(filename, doc.to_xml)

That was the solution: 那是解决方案:

def self.remove_orgname(artinfo)
    # Entferne $titelbild des Artikels
    puts 'Entferne Logo aus dem Article_Info File. Wird anders gesetzt.'
    doc = Nokogiri::XML(IO.read(artinfo))
    doc.search('orgname').each do |node|
      node.remove 
      node.content = 'Children removed'
    end
    IO.write(artinfo, doc.to_xml)
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM