使用Nokogiri将带有名称空间的节点添加到XML文件中

时间:2009-08-28 06:50:27

标签: xml ruby nokogiri

我在编辑XML文件时遇到问题。我目前正在尝试使用Nokogiri,但我可以使用任何其他Ruby库来解决这个问题。

我正在尝试在另一个节点集中添加一个Node集。两者都有一些有趣的命名空间。这是代码。我正在尝试在第一个<p:sp>

之后将new_node添加到父级
require 'rubygems'
require 'nokogiri'

parent = <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*">
  <p:spTree>
    <p:sp>
      <p:nvSpPr>
        <p:cNvPr id="1" name="Title 1"/>
      </p:nvSpPr>
    </p:sp>
  </p:spTree>
</p:sld>
EOF

new_node = <<EOF 
<p:sp>
  <p:cNvPr id="2" name="Title 2"/>
  <a:off x="1524000" y="4572000"/>
</p:sp>
EOF

@doc = Nokogiri::XML(parent)
@doc.xpath('.//p:sp').after(new_node)
在上面的代码运行之后,

@doc看起来像下面的XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*">
  <p:spTree>
    <p:sp>
      <p:nvSpPr>
        <p:cNvPr id="1" name="Title 1"/>
      </p:nvSpPr>
    </p:sp>

   <p:p:sp>
    <p:p:cNvPr name="Title 2" id="2"/>
    <p:a:off x="1524000" y="4572000"/>
   </p:p:sp>

  </p:spTree>
</p:sld>

注意它再次命名为p:下的所有内容。两个节点应该是<p:sp><a:off>而不是<p:p:sp><p:a:off>我可以从new_node中删除p:但是a:off仍将在p下命名为:它不可能。我知道我一定做错了。我正在寻找的最终结果是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*">
  <p:spTree>
    <p:sp>
      <p:nvSpPr>
        <p:cNvPr id="1" name="Title 1"/>
      </p:nvSpPr>
    </p:sp>
    <p:sp>
      <p:cNvPr name="Title 2" id="2"/>
      <a:off x="1524000" y="4572000"/>
    </p:sp>
  </p:spTree>
</p:sld>

1 个答案:

答案 0 :(得分:2)

看起来像Nokogiri是个问题。 Hpricot来救援! (RIP _why)

#!/usr/bin/ruby    
require 'rubygems'
require 'hpricot'

parent = <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*">
  <p:spTree>
    <p:sp>
      <p:nvSpPr>
        <p:cNvPr id="1" name="Title 1"/>
      </p:nvSpPr>
    </p:sp>
  </p:spTree>
</p:sld>
EOF

new_node = <<EOF 
  <p:sp>
    <p:cNvPr id="2" name="Title 2"/>
    <a:off x="1524000" y="4572000"/>
  </p:sp>
EOF


doc = Hpricot(parent)

doc.search('//p:sp').after(new_node)

输出是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld mc:PreserveAttributes="mv:*" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
  <p:sptree>
    <p:sp>
      <p:nvsppr>
        <p:cnvpr name="Title 1" id="1" />
      </p:nvsppr>
    </p:sp>

    <p:sp>
     <p:cnvpr name="Title 2" id="2" />
     <a:off x="1524000" y="4572000" />
    </p:sp>

  </p:sptree>
</p:sld>