Assuming you already have the trimmed contents somewhere and you don't mind that the order is not kept, this might work. The content of the <content> tag will be skipped by expat so it should not waste memory:

use strict; use XML::Rules; my @contents = ('first trimmed content', 'second trimmed content'); my $parser = XML::Rules->new( style => 'filter', start_rules => { content => 'skip', }, rules => { _default => 'raw', product => sub { my ($tag, $attr, $parser) = @_[0,1,4]; $attr->{content} = [ $contents[ $parser->{pad}++ ] ]; return $tag => $attr; } }, ); $parser->filter(\*DATA); __DATA__ <document> <product> <date>2008-10-15</date> <price>124</price> <content>heinous amount of unwanted text</content> <color>red</color> </product> <product> <date>2009/01/30</date> <price>10</price> <content>heinous amount of unwanted text</content> <color>black</color> </product> </document>

Or better formatted, but with even less defined order of child tags of <product> and ¡assuming all those child tags have no attributes or children! :

use strict; use XML::Rules; my @contents = ('first trimmed content', 'second trimmed content'); my $parser = XML::Rules->new( style => 'filter', ident => ' ', stripspaces => 3, start_rules => { content => 'skip', }, rules => { _default => 'content array', product => sub { my ($tag, $attr, $parser) = @_[0,1,4]; $attr->{content} = [ $contents[ $parser->{pad}++ ] ]; return $tag => $attr; } }, ); $parser->filter(\*DATA); __DATA__ <document> <product> <date>2008-10-15</date> <price>124</price> <content>heinous amount of unwanted text</content> <color>red</color> </product> <product> <date>2009/01/30</date> <price>10</price> <content>heinous amount of unwanted text</content> <color>black</color> </product> </document>

If the order is important you'd have to take the first code and tweak the handler of the <product> tag to insert the content at the right place of the array @{$attr->{_content}}.


In reply to Re: Prune Twig From Huge XML File by Jenda
in thread Prune Twig From Huge XML File by andergoo

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.