Sorry, still not enough details. Why do you only include 1 record in the output, when there are 2 properties "TABLE" in the input? Also the code you wrote does not output XML, but a list of property name/values, so your expected result should be at least consistent with that.
Plus (going back to rant mode) why did you truncate the usersql content? I would appreciate not having to track it down in the input to cut and paste it.
That said, the input XML seems incredibly clumsy. Faced with something like this, my first instinct would be to turn all those Subrecords that include a single property defined by 2 Property elements into a single element, named after the property name, with the value of the property as content. Once that's done it should be much easier to work with the data.
The code below does just that.
#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $t= XML::Twig->new( twig_roots => { 'Collection[@Type="CustomProper +ty"]' => \&collection}, twig_print_outside_roots => 1, ); $t->parsefile( $ARG[0]); exit; sub collection { my ($t, $collection)= @_; foreach my $sub_record ($collection->children( 'SubRecord')) { my $name = $sub_record->field( 'Property[@Name="Name"]'); my $value = $sub_record->field( 'Property[@Name="Value"]'); # we want to keep thæ attributes (except Name of course) my $atts = $sub_record->first_child( 'Property[@Name="Value"] +')->atts; delete $atts->{Name}; my $property= XML::Twig::Elt->new( $name, $atts, $value); $property->replace( $sub_record); } $collection->print; }
In reply to Re^3: XML::Twig usage incomprehension
by mirod
in thread XML::Twig usage incomprehension
by Jerome
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |