Not exactly the order you specified in the example, but a correct order anyway:
use strict; use warnings; no warnings 'uninitialized'; use XML::Rules; my %written; my %depends; my $parser = XML::Rules->new( style => 'filter', rules => { _default => 'raw', 'name,members' => 'raw extended', 'Group' => 'raw extended array', record => sub { my @I_depend; if (exists $_[1]->{':members'} and exists $_[1]->{':member +s'}{':Group'}) { @I_depend = grep !exists $written{$_}, map $_->{_conte +nt}, @{$_[1]->{':members'}{':Group'}}; } if (@I_depend) { $_[1]->{':I_depend'} = {map {$_ => 1} @I_depend}; foreach (@I_depend) { push @{$depends{$_}}, $_[1] } return; } else { my $name = $_[1]->{':name'}{_content}; $written{$name} = 1; my @to_write = $_[1]; if (exists $depends{$name}) { push @to_write, find_dependent($name); } return 'record' => \@to_write; } } } ); sub find_dependent { my $name = shift; my @to_write; foreach my $parent (@{$depends{$name}}) { delete $parent->{':I_depend'}{$name}; if (! %{$parent->{':I_depend'}}) { # if it doesn't depend on a +nything more push @to_write, $parent; push @to_write, find_dependent($parent->{':name'}{_content +}); } } delete $depends{$name}; return @to_write; } $parser->filter(\*DATA); if (%depends) { print STDERR "Unsatisfied dependencies! Some records depend on the +se unknown groups: ", join ", ", keys %depends; }
Only if all the records depend (directly or indirectly) on the last one, will the script keep at one moment all the data in memory. Otherwise whenever it finds a record with no dependencies or whose all dependencies were already printed, it will print that record and all records that only depended on this one (and those already printed).
In reply to Re: Parent/Child Group Relationship in XML Imports
by Jenda
in thread Parent/Child Group Relationship in XML Imports
by onegative
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |