Hello, I have to process an XML SELECTIVELY. Namely, if we have field firstname within title with different first letter as arguments, I want to print only those titles that DO NOT have first name with first letter 'A'. For that I use the following code:

use strict; use XML::Twig; my $sel = 'title'; my $ign = 'title/name/fnm[@letter="a"]'; my $twig = XML::Twig->new( ignore_elts => { "$ign" => 1}, twig_roots => { "$sel" => 1}, pretty_print => 'indented' ); $twig->parsefile('1.xml'); $twig->print;

on the following XML:

<?xml version="1.0"?> <titles> <title id="1">testing<name><snm>Houston</snm>, <fnm letter="c">Carl</f +nm></name><g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14 +</blk></title> <title id="2">testing<name><snm>Houston</snm>, <fnm letter="a">Allan</ +fnm></name><g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>1 +4</blk></title> <title id="3">testign <name>Houston <fnm j="b">Bob</fnm> <g>49</g> <title> testing the sub title in title </title> <title> this is pcdata in subtitle <title> second sub level </title> </title> </name> </title> <test> This is a test </test> </titles>

I am new to XML::Twig. I read about ignore_elts and I though it will do the job. It does, but not exactly what I want. So basically what I want is to ignore the whole title where the ignore condition is met:

<?xml version="1.0"?> <titles> <title id="1">testing<name><snm>Houston</snm>, <fnm letter="c">Carl< +/fnm></name><g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk> +14</blk></title> <title id="3">testign <name>Houston <fnm j="b">Beb</fnm><g>49</g><title> testing the sub title in title </title><title> this is pcdata in subtitle <title> second sub level </title></title></name></title> </titles>

while what I get is (the title with id=2 is there but the fnm is missing):

<?xml version="1.0"?> <titles> <title id="1">testing<name><snm>Houston</snm>, <fnm letter="c">Carl< +/fnm></name><g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk> +14</blk></title> <title id="2">testing<name><snm>Houston</snm>, </name><g>69</g><ppg> +20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk></title> <title id="3">testign <name>Houston <fnm j="b">Beb</fnm><g>49</g><title> testing the sub title in title </title><title> this is pcdata in subtitle <title> second sub level </title></title></name></title> </titles>

Any suggestions?
Thank you very much in advance.
Regards,
Dimitar


In reply to xml::twig twig_roots / ignore_elts by dimitarsh1

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.