# XML Manipulating

**URL:** <https://www.ruby-forum.com/t/xml-manipulating/181465>\
**Category:** Ruby\
**Created:** [January 12, 2010, 12:44pm UTC](https://www.ruby-forum.com/t/xml-manipulating/181465 "2010-01-12T12:44:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [January 12, 2010, 12:44pm UTC](https://www.ruby-forum.com/t/xml-manipulating/181465/1 "2010-01-12T12:44:40Z")

</div>

Hi everybody

```
    Can anybody there help me in solving the following issue.

```

Consider the XML

```
  <row>
     <column>Business Strategy</column>
     <column>bus1</column>
  </row>
  <row>
     <column>Company Description</column>
     <column>tar1</column>
  </row>
  <row>
      <column>Business Strategy</column>
      <column>bus2</column>
  </row>
  <row>
      <column>Company Description</column>
      <column>tar2</column>
  </row>
  <row>
     <column>Business Strategy</column>
     <column>bus3</column>
  </row>
  <row>
     <column>Company Description</column>
     <column>tar3</column>
  </row>

```

Can this xml be parsed and manipulated in ruby as

```
  <row>
     <column>Business Strategy</column>
     <column>bus1</column>
     <column>bus2</column>
     <column>bus3</column>
  </row>
  <row>
     <column>Company Description</column>
     <column>tar1</column>
     <column>tar2</column>
     <column>tar3</column>
  </row>

```

Any help would be greatly appreciated.

Thanks in advance

Anandh

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [January 12, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/xml-manipulating/181465/2 "2010-01-12T13:06:16Z")

</div>

El Martes, 12 de Enero de 2010, Anandh K. escribiÃ³:

> ```
> <row>
> </row>
> 
> <column>tar3</column>
> </row>
> 
> ```
> 
> Any help would be greatly appreciated.

Of course it can be manipulated to get your desired output. You must use  
a XML  
parser (I recommend Nokogiri), inspect the XML by extracting/moving the  
nodes  
you are interested in and generating the new XML.

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [January 12, 2010, 2:57pm UTC](https://www.ruby-forum.com/t/xml-manipulating/181465/3 "2010-01-12T14:57:27Z")

</div>

Hi Anandh,

I’ve cross-posted my reply to nokogiri-talk, which is where followup  
questions are probably best sent.

On Tue, Jan 12, 2010 at 6:44 AM, Anandh K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ```
> <row>
> </row>
> 
> <column>tar3</column>
> </row>
> 
> ```
> 
> Any help would be greatly appreciated.

This is straightforward if you know some XPath (see  
[xpath cover page](http://www.w3.org/TR/xpath)) and Nokogiri.

I’ve posted a solution at:

> <https://gist.github.com/flavorjones/275209>

The general idea is that we find the nodes we care about, and then build  
a  
new document with those contents.

Cheers,  
-mike

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [January 13, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/xml-manipulating/181465/4 "2010-01-13T13:31:54Z")

</div>

Anandh  
Â  
Apply an XSLT with Ruby On Rails.  
Please refer  
[http://snippets.dzone.com/posts/show/4615](http://snippets.dzone.com/posts/show/4615)  
Â  
[http://www.xml.com/pub/a/2007/01/17/making-xml-in-a-rails-app-xml-builder.html](http://www.xml.com/pub/a/2007/01/17/making-xml-in-a-rails-app-xml-builder.html)  
Â  
thanks  
Deepak

— On Tue, 1/12/10, Anandh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

From: Anandh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: XML Manipulating  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Tuesday, January 12, 2010, 3:44 AM

Hi everybody

Â Â Â Â Can anybody there help me in solving the following issue.

Consider the XML

Â Â Â   
Â Â Â Â Â Â Business Strategy  
Â Â Â Â Â Â bus1  
Â Â Â   
Â Â Â   
Â Â Â Â Â Â Company Description  
Â Â Â Â Â Â tar1  
Â Â Â   
Â Â Â   
Â Â Â Â Â Business Strategy  
Â Â Â Â Â bus2  
Â Â Â   
Â Â Â   
Â Â Â Â Â Company Description  
Â Â Â Â Â tar2  
Â Â Â   
Â Â Â   
Â Â Â Â Â Â Business Strategy  
Â Â Â Â Â Â bus3  
Â Â Â   
Â Â Â   
Â Â Â Â Â Â Company Description  
Â Â Â Â Â Â tar3  
Â Â Â

Can this xml be parsed and manipulated in ruby as

Â Â Â   
Â Â Â Â Â Â Business Strategy  
Â Â Â Â Â Â bus1  
Â Â Â Â Â Â bus2  
Â Â Â Â Â Â bus3  
Â Â Â   
Â Â Â   
Â Â Â Â Â Â Company Description  
Â Â Â Â Â Â tar1  
Â Â Â Â Â Â tar2  
Â Â Â Â Â Â tar3  
Â Â Â

Any help would be greatly appreciated.

Thanks in advance

Anandh

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [January 20, 2010, 1:14pm UTC](https://www.ruby-forum.com/t/xml-manipulating/181465/5 "2010-01-20T13:14:58Z")

</div>

\<?xml version="1.0"?\> Business Strategy aaaa aaaaa gggg \<?xml version="1.0"?\> Company Description bbbb bbbb fhghj \<?xml version="1.0"?\> Business Strategy aaaa aaaaa gggg \<?xml version="1.0"?\> Company Description bbbb bbbb fhghj

Consider the above XML. Is there anyway to remove “\<?xml version="1.0"?\>” and also the duplicate block with same data.

For example consider the following identical blocks

\<?xml version="1.0"?\> Business Strategy aaaa aaaaa gggg \<?xml version="1.0"?\> Business Strategy aaaa aaaaa gggg

The resulting xml should remove the duplicate block and should contain  
only one.

Any help would be appreciated.

Thanks in Advance

Anandh

Anandh K. wrote:

> Thanks mike. The solution solved my problem to a greater extent but with  
> one issue.
> 
> In the resulting solution which is as follows
> 
> Business Strategy bus1 bus2 bus3 Company Description tar1 tar2 tar3
> 
> Can you suggest me how can i avoid “\<?xml version="1.0"?\>”.
> 
> If i give some thing like this
> 
> puts new\_doc.to\_xml(:dasherize =\> false, :skip\_instruct=\>true)
> 
> Ruby compiler threw argument error.
> 
> Any help would be appreciated.
> 
> Thanks
> 
> Anandh
> 
> Mike D. wrote:
> 
> > Hi Anandh,
> > 
> > I’ve cross-posted my reply to nokogiri-talk, which is where followup  
> > questions are probably best sent.
> > 
> > On Tue, Jan 12, 2010 at 6:44 AM, Anandh K.  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> > 
> > > ```
> > > <row>
> > > </row>
> > > 
> > > <column>tar3</column>
> > > </row>
> > > 
> > > ```
> > > 
> > > Any help would be greatly appreciated.
> > 
> > This is straightforward if you know some XPath (see  
> > [xpath cover page](http://www.w3.org/TR/xpath)) and Nokogiri.
> > 
> > I’ve posted a solution at:
> > 
> > [xml.rb · GitHub](http://gist.github.com/275209)
> > 
> > The general idea is that we find the nodes we care about, and then build  
> > a  
> > new document with those contents.
> > 
> > Cheers,  
> > -mike

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [January 20, 2010, 10:06am UTC](https://www.ruby-forum.com/t/xml-manipulating/181465/6 "2010-01-20T10:06:23Z")

</div>

Thanks mike. The solution solved my problem to a greater extent but with  
one issue.

In the resulting solution which is as follows

Business Strategy bus1 bus2 bus3 Company Description tar1 tar2 tar3

Can you suggest me how can i avoid “\<?xml version="1.0"?\>”.

If i give some thing like this

puts new\_doc.to\_xml(:dasherize =\> false, :skip\_instruct=\>true)

Ruby compiler threw argument error.

Any help would be appreciated.

Thanks

Anandh

Mike D. wrote:

> Hi Anandh,
> 
> I’ve cross-posted my reply to nokogiri-talk, which is where followup  
> questions are probably best sent.
> 
> On Tue, Jan 12, 2010 at 6:44 AM, Anandh K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > ```
> > <row>
> > </row>
> > 
> > <column>tar3</column>
> > </row>
> > 
> > ```
> > 
> > Any help would be greatly appreciated.
> 
> This is straightforward if you know some XPath (see  
> [xpath cover page](http://www.w3.org/TR/xpath)) and Nokogiri.
> 
> I’ve posted a solution at:
> 
> [xml.rb · GitHub](http://gist.github.com/275209)
> 
> The general idea is that we find the nodes we care about, and then build  
> a  
> new document with those contents.
> 
> Cheers,  
> -mike

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [January 22, 2010, 7:38am UTC](https://www.ruby-forum.com/t/xml-manipulating/181465/7 "2010-01-22T07:38:06Z")

</div>

Dear Mike

```
Can this be done using Hpricot. I tried doing with Hpricot. But 

```

Hpricot doesnot support xpath. It support something called “search”.With  
that too I could not get my desired result since it doesnot support  
“following-sibling” property.

Any help would be appreciated.

Thanks

Anandh

Mike D. wrote:

> Hi Anandh,
> 
> I’ve cross-posted my reply to nokogiri-talk, which is where followup  
> questions are probably best sent.
> 
> On Tue, Jan 12, 2010 at 6:44 AM, Anandh K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > ```
> > <row>
> > </row>
> > 
> > <column>tar3</column>
> > </row>
> > 
> > ```
> > 
> > Any help would be greatly appreciated.
> 
> This is straightforward if you know some XPath (see  
> [xpath cover page](http://www.w3.org/TR/xpath)) and Nokogiri.
> 
> I’ve posted a solution at:
> 
> [xml.rb · GitHub](http://gist.github.com/275209)
> 
> The general idea is that we find the nodes we care about, and then build  
> a  
> new document with those contents.
> 
> Cheers,  
> -mike
