Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 349781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:29:25+00:00 2026-05-12T11:29:25+00:00

I’m having trouble editing an XML file. I’m currently trying to use Nokogiri ,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:29:25+00:00Added an answer on May 12, 2026 at 11:29 am

    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">
      <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I am trying to understand how to use SyndicationItem to display feed which is
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.