how to split xml using xpath in perl? -
i have input xml have split based on doc , delt wise , save in format delt_0001.xml
this code
#!/usr/bin/perl use xml::xpath; $file = 'file.xml'; $xp = xml::xpath->new(filename=>$file); foreach $entry ( $xp->findnodes('/xml/service/main/doc') ) { $filename = $entry->findvalue('./delt/@id'); foreach $entry1( $entry->findnodes('//delt')){ $filename = $entry1->findvalue('/delt/@id'); $content = $entry1->tostring; open(wr,">delt_$filename.xml"); print wr "$content\n"; close wr; }
when run program delt portion prints in 1 xml.
input xml delt.xml
<xml> <service> <title>split xml</title> <main> <doc id="001"> <title>doc1</title> <delt id="0001"> <title>delt1</title> <text>num1</text>`` <text>num1</text> </delt> <delt id="0002-a"> <title>delt1</title> <text>num1</text> <text>num1</text> </delt> </doc> <doc id="002"> <title>doc2</title> <delt id="0003"> <title>delt1</title> <text>num1</text> <text>num1</text> </delt> <delt id="0004"> <title>delt1</title> <text>num1</text> <text>num1</text> </delt> </doc> </main> </service> </xml>
output geting
<delt id="0001"> <title>delt1</title> <text>num1</text>`` <text>num1</text> </delt> <delt id="0002-a"> <title>delt1</title> <text>num1</text> <text>num1</text> </delt> <delt id="0003"> <title>delt1</title> <text>num1</text> <text>num1</text> </delt> <delt id="0004"> <title>delt1</title> <text>num1</text> <text>num1</text> </delt>
output needed
split no 1 delt_0001.xml
<xml> <service> <title>split xml</title> <main> <doc id=001> <title>doc1</title> <delt id=0001> <title>delt1</title> <text>num1</text>`` <text>num1</text> </delt> </doc> </main> </service> </xml>
split no 2 delt_0002-a.xml
<xml> <service> <title>split xml</title> <main> <doc id=001> <title>doc1</title> <delt id=0002=a> <title>delt1</title> <text>num1</text> <text>num1</text> </delt> </doc> </main> </service> </xml>
split no 3 delt_0003.xml
<xml> <service> <title>split xml</title> <main> <doc id=002> <title>doc2</title> <delt id=0003> <title>delt1</title> <text>num1</text> <text>num1</text> </delt> </doc> </main> </service> </xml>
split no 4 delt_0004.xml
<xml> <service> <title>split xml</title> <main> <doc id=002> <title>doc2</title> <delt id=0004> <title>delt1</title> <text>num1</text> <text>num1</text> <delt> </doc> </main> </service> </xml>
thanks in advance
it's simple xml::twig (and happy got "delete current element during parsing" work while back):
#!/usr/bin/perl use strict; use warnings; use xml::twig; $delt= 'delt.xml'; xml::twig->new( twig_handlers => { delt => \&delt }, pretty_print => 'indented', ) ->parsefile( $delt); exit; sub delt { my( $t, $delt)= @_; $delt_file= sprintf( 'delt_%s.xml', $delt->id); # tricky part: remove previous doc if needed if( $prev_doc= $delt->parent( 'doc')->prev_sibling( 'doc')) { $prev_doc->delete; } $t->print_to_file( $delt_file); $delt->delete; }
Comments
Post a Comment