I'm a little confused using the Perl XML::Twig module. What I'm trying to do is output a subset of the matched twig_root entries in an XML file. Essentially I call a method for the 'definition' twig_root and then I only want to output some of the definitions based on a condition. Of course I also want the root of the XML document to be output. It isn't. What am I doing wrong? I expect the <oval> tag and the selected <definition> and nothing else. I'm using the root <oval> tag.

XML

===

<?xml version="1.0" encoding="UTF-8"?> <oval xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval#redhat redhat-schema.xsd http://oval.mitre.org/XMLSchema/oval#windows windows-schema.xsd http://oval.mitre.org/XMLSchema/oval#unix unix-schema.xsd http://oval.mitre.org/XMLSchema/oval#independent independent-schema.xsd http://oval.mitre.org/XMLSchema/oval#solaris solaris-schema.xsd http://oval.mitre.org/XMLSchema/oval oval-schema.xsd" xmlns:oval="http://oval.mitre.org/XMLSchema/oval" xmlns="http://oval.mitre.org/XMLSchema/oval" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:redhat="http://oval.mitre.org/XMLSchema/oval#redhat" xmlns:windows="http://oval.mitre.org/XMLSchema/oval#windows"xmlns:sola +ris="http://oval.mitre.org/XMLSchema/oval#solaris"> <generator> <schema_version>4.2</schema_version> <timestamp>20051212102623</timestamp> </generator> <definitions> <definition id="OVAL2" class="vulnerability"> <affected family="redhat"> <redhat:platform>Red Hat Linux 9</redhat:platform> <product>Mutt</product> </affected> </definition> <definition id="OVAL3" class="vulnerability"> <affected family="windows"> <windows:platform>Microsoft Windows 2000</windows:platform> <windows:platform>Microsoft Windows Server 2003</windows:platform> <product>Microsoft Exchange Server 2003</product> </affected> </definition> <definition id="OVAL6" class="vulnerability"> </definition> </definitions> <crap>This is some crap</crap> </oval>

Perl code to extract a subset of the definitions

==================================

use XML::Twig; my $doc=new XML::Twig ( twig_roots => { "definition" => \&parseEntry } ); sub parseEntry { my ($twig,$element)=@_; my $id=$element->att("id"); if ($id eq "OVAL3") { $element->flush(); return 1; } $twig->purge(); return 0; } $doc->parsefile("test.xml"); $doc->flush();

The output

========

<definition class="vulnerability" id="OVAL3"> <affected family="windows"> <windows:platform>Microsoft Windows 2000</windows:platform> <windows:platform>Microsoft Windows Server 2003</windows:platform> <product>Microsoft Exchange Server 2003</product> </affected> </definition> </oval>

Edit: g0n - readmore tags


In reply to XML::Twig question by rjkoop

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.