1

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

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

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>
4

1 回答 1

2

所以看起来Nokogiri是问题所在。Hpricot 来救援!(RIP_为什么)

#!/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>
于 2009-08-30T01:59:10.730 回答