Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Nokogiri将具有名称空间的节点添加到XML文件_Xml_Ruby_Nokogiri - Fatal编程技术网

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

使用Nokogiri将具有名称空间的节点添加到XML文件,xml,ruby,nokogiri,Xml,Ruby,Nokogiri,编辑XML文件时遇到问题。我目前正在尝试使用,但我愿意使用任何其他Ruby库来解决这个问题 我正在尝试在另一个节点集中添加一个节点集。两者都有一些有趣的名称空间。这是密码。我正试图在第一个 需要“rubygems” 需要“nokogiri” parent=所以看起来Nokogiri是个问题。救命啊!(为什么) #/usr/bin/ruby 需要“rubygems” 需要“hpricot” parent=来自Nokogiri主页:“XML就像暴力——如果它不能解决您的问题,您就没有充分使用它。”

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

我正在尝试在另一个节点集中添加一个节点集。两者都有一些有趣的名称空间。这是密码。我正试图在第一个

需要“rubygems”
需要“nokogiri”

parent=所以看起来Nokogiri是个问题。救命啊!(为什么)

#/usr/bin/ruby
需要“rubygems”
需要“hpricot”
parent=来自Nokogiri主页:“XML就像暴力——如果它不能解决您的问题,您就没有充分使用它。”
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)
<?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>
<?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>
#!/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>