I am trying to use XML::Twig to convert a policy (call it a firewall policy, but that's not strictly true) from one format to another. Call the existing file format "OLDFW" and the new format "NEWFW." I have boiled the problem down to bare-essentials XML below.

OLDFW has a root called "policy" which contains one element and one only: "policyrules". That element contains many "Rule" elements which in turn have sub-elements, etc.

NEWFW should be the same (actually there are changes, but I don't need help with that), except the <policyrules> element is eliminated -- that is, all the "Rule" elements are directly underneath "<policy>".

I know that if I simply ignore or delete <policyrules>, I lose all the children. I want to build a handler to skip over it, but that's the problem.

I have kludged around the problem by filtering out <policyrules> before XML-parsing the file, but I am now wondering whether there is a more elegant solution where I can simply tell XML::Twig (or whatever) to "promote" all the "Rule" elements to the same level as "policyrules" and then delete the latter.

Here's a sample OLDFW:

&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;policy name="Cur_policy" version="3.2.1"&gt; &lt;policyrules&gt; &lt;Rule Action="allow" Enabled="true" RuleID="F68E32"&gt; &lt;source addr="10.4.5.3" srcport="any"/&gt; &lt;dest addr="199.5.4.3" destport="80"/&gt; &lt;/Rule&gt; &lt;Rule Action="allow" Enabled="true" RuleID="78E21D"&gt; &lt;source addr="10.4.0.0-10.4.255.255" srcport="any"/&gt; &lt;dest addr="any" destport="80"/&gt; &lt;/Rule&gt; &lt;/policyrules&gt; &lt;/policy&gt;

The NEWFW I'd like to get:

&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;policy name="Cur_policy" version="3.2.1"&gt; &lt;Rule Action="allow" Enabled="true" RuleID="F68E32"&gt; &lt;source addr="10.4.5.3" srcport="any"/&gt; &lt;dest addr="199.5.4.3" destport="80"/&gt; &lt;/Rule&gt; &lt;Rule Action="allow" Enabled="true" RuleID="78E21D"&gt; &lt;source addr="10.4.0.0-10.4.255.255" srcport="any"/&gt; &lt;dest addr="any" destport="80"/&gt; &lt;/Rule&gt; &lt;/policy&gt;

The code I'm playing with -- what I need is "what goes into policyrules_handler?"

#!/usr/bin/perl use warnings; use strict; use XML::Twig; my ( $in_XML_file, $out_XML_file ) = @ARGV; # create objects my $ref_reader = XML::Twig-&gt;new( twig_handlers =&gt; { 'policyrules' =&gt; \&policyrules_ +handler, 'Rule' =&gt; \&rule_handler } ); $ref_reader-&gt;parsefile( $in_XML_file ) or die " $in_XML_file is not parsable\n"; open my $out_fh, '&gt;', $out_XML_file or die " $out_XML_file cannot be written: $!"; $ref_reader-&gt;print( $out_fh ); close $out_fh; sub policyrules_handler { my( $t, $elt)= @_; # arguments for all twig_handlers # $elt-&gt;delete; } sub rule_handler { my( $t, $elt)= @_; # arguments for all twig_handlers $elt-&gt;set_att( 'new_attr' =&gt; 'dummyValue' ); }

Thank you for any assistance you can provide here.

jd


In reply to Remove level of elements (preserving their children) in XML::Twig? by jdbaldwin23

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.