rkg has asked for the wisdom of the Perl Monks concerning the following question:
I am new to XML::Twig. A neat module indeed.
I have a question about stream vs. in-memory processing.
I am using Twig to change the form of an XML document. The DTD is complex and the document is large; here's an abstraction of the base document.
I want to promote each "C" into its own "B", cloning the parent and making each kid an only-child, yielding something like this#### HAND EDITED, NOT TESTED <a> <b name="funny words"> <c name="foo"/> <c name="baz"/> </b> <b name="foods"/> <c name="apple"/> <c name="pear"/> <c name="cheese"/> </b> </a>
The Twig code I've written works, and is something like this#### HAND EDITED, NOT TESTED <a> <b name="foo"> <c name="foo"/> </b> <b name="baz"> <c name="baz"/> </b> <b name="apple"> <c name="apple"/> </b> <b name="pear"> <c name="pear"/> </b> <b name="cheese"> <c name="cheese"/> </b> </a>
My questions:#### HAND EDITED my $t = XML::Twig->new( twig_handlers => { b => \&b, }, pretty_print => 'indented', ); $t->parsefile($xml_file); $t->flush; sub b { my ( $t, $x ) = @_; my @c = $x->children('c'); my %bs; foreach my $c (@c) { my $text = $c->att('name'); $c->cut; push ( @{ $bs{$text} }, $c ); } foreach my $text ( keys %bs ) { my $b = $x->insert_new_elt( 'after', 'b', { %{ $x->atts } } ); $b->set_att( 'name' => $text ); foreach ( @{ $bs{$text} } ) { $adg->insert_new_elt( 'first_child', 'c', { %{ $_->atts } } ); } } $x->delete; } }
PS When I said "hand edited" above, I mean I took working code and working XML and simplified them for this post -- possible a typo crept in during the simplification for the post. But the original works and changes the original XML appropriately.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML:Twig -- changing in-mem process to stream
by mirod (Canon) on Oct 15, 2003 at 11:48 UTC | |
|
Re: XML:Twig -- changing in-mem process to stream
by rkg (Hermit) on Oct 15, 2003 at 09:49 UTC |