Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: How can I keep or discard certain blocks of an XML file based on first line of block?

by Anonymous Monk
on Jun 26, 2014 at 21:42 UTC ( [id://1091407]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How can I keep or discard certain blocks of an XML file based on first line of block?
in thread How can I keep or discard certain blocks of an XML file based on first line of block?

I discovered one more wrinkle. I didn't think the label_y blocks at the top of the file were important, but it turns out they are. Remember the format of those is:

<label_y="somevalue"> <label_z>a value</label_z> <label_z>a value</label_z> <label_z>a value</label_z> <label_z>a value</label_z> </label_y>

What I need to do is this:

Discard all but the first (or last, doesn't matter) of these, so there is only one such block in the file. Or, discard all of them and create a replacement - it would not really matter, just so there is only one.

If keeping one of the original blocks, save "somevalue" from the first line in a variable such as $somevalue.

Then, lower in the program, before writing the "kept" label_a blocks to the output file, I need to do this:

$e->set_att( 'channel' => $somevalue );

Since what's been posted so far is far more elegant than anything I would have come up with, I'd be interested in knowing a good way to do discard all (or all but one) of those initial blocks, and insert a replacement if needed, so there is only one.

By the way, relative to my previous post I did discover keep_atts_order => 'TRUE', which works in Linux but for some reason not in ActivePerl in Windows.

  • Comment on Re^3: How can I keep or discard certain blocks of an XML file based on first line of block?
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: How can I keep or discard certain blocks of an XML file based on first line of block?
by poj (Abbot) on Jun 27, 2014 at 06:56 UTC
    What do you have in <...other lines that must be copied...> ?, and what is the attribute name in <label_y="somevalue">, that is not valid XML.
    To remove the duplicate blocks, add a handler for the label_y tags to cut the element and then print at the end using an end_tag_handler.
    my $label_y; my $twig = XML::Twig->new( twig_roots => { 'label_a' => \&label_a , 'label_y' => \&label_y , }, , end_tag_handlers => { 'label_x' => \&label_x, }, twig_print_outside_roots => $OUT, # print rest ); sub label_y { my ($twig,$e) = @_; $label_y = $e->cut; } sub label_x { my ($twig,$e) = @_; my $y = $label_y->att('y'); $label_y->set_att( channel => $y); $label_y->print; }
    poj
Re^4: How can I keep or discard certain blocks of an XML file based on first line of block?
by Anonymous Monk on Jun 26, 2014 at 22:29 UTC
    Okay, I'm an idiot - completely missed the part about needing to install Tie::IxHash. It must have been already installed on the Linux box. After installing it on the Windows box, adding keep_atts_order => 'TRUE', works great! Thank you again!!!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1091407]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-23 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found