2009-08-28 39 views
1

我在编辑XML文件时遇到问题。我目前正在尝试使用Nokogiri,但我向任何其他Ruby库开放以解决此问题。将具有名称空间的节点添加到包含Nokogiri的XML文件中

我试图在另一个节点集内添加一个节点集。两者都有一些有趣的命名空间。这是代码。我想第一个<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之后到new_node添加到父看起来像上面的代码后,下面的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> 
+0

从引入nokogiri主页:“XML是喜欢暴力 - 如果它不能解决你的问题,你是不是用够了。 “ – 2009-08-28 08:41:18

回答

1

所以看起来像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> 
相关问题