tags:

views:

43

answers:

2

Suppose the XML file is:

<class name=math>
<student>luke1</student>
...
<student>luke8000000</student>
</class>
<class name=english>
<student>mary1</student>
...
<student>mary1000000</student>
</class>

after class=math is parsed, I want to delete this element from the XML file so that when class=english is parsed, Twig will not go through the content of class=math.

The reason I want to do this is that, so far even if I use TwigRoots => {"class[\@name='english']" => \&counter} I still need to wait a long time for Twig to start to parse class=english because it needs to go over each line of class=math (correct me if it does not need to go over each line). In the actual file I run, there are several classes, I do not want to let Twig go through each line in class=math before finding the class it is really interested in.

Thanks in advance.

A: 

I havent used the delete functionality with TWIG but check this link This has some infor about deleteion of nodes using TWIG

The relevant portion being here:

    }
    else {
        $para->delete;
    }
}

The last part of the paragraph handler deletes the twig from the result tree if the paragraph didn't contain a match for the specified keyword. This ensures that only those paragraphs containing a match will make it into the final output.

$para is an element passed into the handler.

Raghuram
Thankns, I tried delete. it deletes element from memory not from xml.
+3  A: 

Could you use the ignore_elts option when you build the twig:

ignore_elts
   This option lets you ignore elements when building the twig. This is useful
   in cases where you cannot use "twig_roots" to ignore elements, for example 
   if the element to ignore is a sibling of elements you are interested in.

           Example:

             my $twig= XML::Twig->new( ignore_elts => { elt => 1 });
             $twig->parsefile( 'doc.xml');

  This will build the complete twig for the document, except that all "elt" 
  elements (and their children) will be left out.

In this case you could write XML::Twig->new( ignore_elts => { 'class[@name="math"]' => 1 }, ... to skip those elements

Note that these elements will not be included in the tree but they will still be parsed. That speeds up things a bit, but not that much (how's that for quantitative data? ;--) In any case the whole file needs to be parsed.

BTW the XML in your question is not well-formed, there should be quotes around attributes.

mirod