XML Manipulating

A

Anandh Kumar

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
 
I

Iñaki Baz Castillo

El Martes, 12 de Enero de 2010, Anandh Kumar escribi=C3=B3:
Hi everybody
=20
Can anybody there help me in solving the following issue.
=20
Consider the XML
=20
<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>
=20
Can this xml be parsed and manipulated in ruby as
=20
<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>
=20
Any help would be greatly appreciated.

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


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
M

Mike Dalessio

[Note: parts of this message were removed to make it a legal post.]

Hi Anandh,

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

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.
This is straightforward if you know some XPath (see
http://www.w3.org/TR/xpath) and Nokogiri.

I've posted a solution at:

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
 
A

Anandh Kumar

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


In the resulting solution which is as follows



<root>
<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>
</root>


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
 
A

Anandh Kumar

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


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"?>
<row>
<column>Business Strategy</column>
<column>aaaa</column>
<column>aaaaa</column>
<column>gggg</column>
</row>

<?xml version="1.0"?>
<row>
<column>Business Strategy</column>
<column>aaaa</column>
<column>aaaaa</column>
<column>gggg</column>
</row>

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

Any help would be appreciated.


Thanks in Advance

Anandh
 
A

Anandh Kumar

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top