tags:

views:

393

answers:

2

Hello all,

I'm having trouble editing an XML file. I'm currently trying to use Nokogiri, but I'm open to any other Ruby library to solve this problem.

I'm trying to add a Node set inside another node set. Both have some interesting namespacing. Here's the code. I'm trying to add the new_node to the parent right after the first <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 looks something like the XML below after the above code runs:

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

Notice it namespaced everything under p: again. The two nodes should be <p:sp> and <a:off> not <p:p:sp> and <p:a:off> I could just remove the p: from the new_node but the a:off would still be namespaced under p: which it can't be. I know I must be doing some wrong. The end result I'm looking for is:

<?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  A: 

So looks like Nokogiri was the problem. Hpricot to the rescue! (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)

And the output is:

<?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"&gt;
  <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>
jpoz
A: 

Why does Hpricot change all the tags to lower case?

Liz