in reply to Re^4: XML::Twig usage incomprehension
in thread XML::Twig usage incomprehension

OK, the code below should do the trick. 2 comments: if you have control over the output format, I would have the TABLE and USERSQL elements merged into 1 element, either a simple one with the table as an attribute, or just an englobing one with 2 sub-elements. These 2 elements are linked, the format should show this. And I had to guess what to do with the GENSQL property that appeared before the USERSQL one for the second table.

#!/usr/bin/perl use strict; use warnings; use XML::Twig::XPath; my $t= XML::Twig::XPath->new( twig_roots => { Job => \&job}, pretty_pr +int => 'indented'); $t->parsefiel( $ARGV[0]); exit; sub job { my ($t, $job)= @_; my @sub_record_table= $job->findnodes( './/SubRecord[./Property[@N +ame="Name" and text()="TABLE"]]'); return unless( @sub_record_table); my $out_job= XML::Twig::Elt->new( job => { identifier => $job->att +( 'Identifier') }); foreach my $sub_record ( @sub_record_table) { my $table = $sub_record->field( 'Property[@Name="Value"]'); $out_job->insert_new_elt( last_child => TABLE => $table); my $found_sql=0; while( !$found_sql) { $sub_record= $sub_record->next_sibling( 'SubRecord') or la +st; my $tag= $sub_record->field( 'Property[@Name="Name"]'); my $content= $sub_record->field( 'Property[@Name="Value"]' +); if( $content ne 'No') { $found_sql=1; $out_job->insert_new_elt( last_child => $tag => $conte +nt); } } } $out_job->print(); $t->purge; # if you need to free the memory }

Replies are listed 'Best First'.
Re^6: XML::Twig usage incomprehension
by Jerome (Initiate) on Apr 03, 2006 at 15:45 UTC
    Thanks a lot!!! It works really fine!

    FYI, I just wanted the TABLE and USERSQL to see if the tables referenced in these 2 fields matches. The other datas are useless in that case.

    I've juste changed the if clause :

    #!/usr/bin/perl use strict; use warnings; use XML::Twig::XPath; my $file = "T:/BI/Jerome/xml/Portage.xml"; my $t= XML::Twig::XPath->new( twig_roots => { Job => \&job}, pretty_pr +int => 'indented'); $t->parsefile( $file ); print_to_file('T:/BI/Jerome/xml/traite.xml'); exit; sub job { my ($t, $job)= @_; my @sub_record_table= $job->findnodes( './/SubRecord[./Property[@N +ame="Name" and text()="TABLE"]]'); return unless( @sub_record_table); my $out_job= XML::Twig::Elt->new( job => { identifier => $job->att +( 'Identifier') }); foreach my $sub_record ( @sub_record_table) { my $table = $sub_record->field( 'Property[@Name="Value"]'); $out_job->insert_new_elt( last_child => TABLE => $table); my $found_sql=0; while( !$found_sql) { $sub_record= $sub_record->next_sibling( 'SubRecord') or la +st; my $tag= $sub_record->field( 'Property[@Name="Name"]'); my $content= $sub_record->field( 'Property[@Name="Value"]' +); #if( $content ne 'No') # { $found_sql=1; # $out_job->insert_new_elt( last_child => $tag => $cont +ent); # } if ( $tag =~ /USERSQL/) { $found_sql = 1; $out_job->insert_new_elt( last_child => $tag => $conte +nt); } } } $out_job->print(); $t->purge; # if you need to free the memory }
    The thing I couldn't get was how to filter the output, and it was by the findnodes() function.

    Really appreciated your help and the time you've spent on my problem.

    Regards.

    Jerome.