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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.