Subject: | after using set_id() and wrap_children(), elt_id() returns incorrect element |
In this testcase, I have two handlers:
A 'title' handler moves the ID value of "ID1" from a title element to its parent.
A 'chapter' handler wraps the contents of the chapter (including the title).
When processing is finished, it is the chapter element that has id==ID1. However, elt_id('ID') incorrectly returns the original title element instead; it is somehow not aware of the ID manipulation by the title handler.
Subject: | bug_id.pl |
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $xml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<manual>
<chapter>
<number/>
<title id="ID1">Title 1</title>
</chapter>
<chapter>
<number/>
<title id="ID2">Title 2</title>
</chapter>
</manual>
EOF
my $twig=XML::Twig->new;
$twig->setTwigHandlers({
'title' => sub {
my $id = $_->id;
$_->del_id;
$_->parent->set_id($id);
return 1; },
'chapter' => sub {
$_->wrap_children('.*', 'body');
return 1; },
});
$twig->parse($xml);
(my $e) = ($twig->get_xpath('//*[@id="ID1"]'));
$twig->print(pretty_print => 'indented');
print "\n\nfinding element id=ID1 by using xpath[\@id='ID1']: ".$e->xpath."\n\n";
print ' finding element id=ID1 by elt_id(\'ID1\'): '.$twig->elt_id('ID1')->xpath."\n";
print ' ^^^ id of this xpath:'.($twig->get_xpath($twig->elt_id('ID1')->xpath))[0]->id."\n";