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>