Subject: | find_nodes() not finding elements |
Run the simple perl script with the attached XML file in the same
directory. The output shows
D:\MyWork\perl_scripts\WishList>perl -w twig.pl
New entry - To Die for
will look for nodes with att - //Item[@A='To Die for']
Found in archive
New entry - To/Die/for
will look for nodes with att - //Item[@A='To/Die/for']
Not found in archive
My question is why the second entry is NOT found in the archive.
It seems to have something to do with the forward slash.
Any ideas?
Thanks,
Ted
Subject: | test.xml |
<?xml version="1.0" ?>
<WishList>
<Item A='To Die for'>
</Item>
<Item A='To/Die/for'>
</Item>
</WishList>
Subject: | twig.pl |
use strict;
use warnings;
use diagnostics;
use XML::Twig;
my $archive_twig;
sub item {
my( $t, $item ) = @_; # arguments for all twig_handlers
my $a = $item->att('A');
print "\n\nNew entry - $a";
my $xp = sprintf( "//Item[\@A='%s']", $a);
print "\nwill look for nodes with att - $xp";
my @elt_array = $archive_twig->findnodes( $xp );
if ( scalar(@elt_array) > 0 ) {
print "\nFound in archive";
} else {
print "\nNot found in archive";
}
}
# we will search this twig for elements found from twig $t
$archive_twig = XML::Twig->new( keep_encoding => 1 );
$archive_twig->parsefile( ".\\test.xml" );
my $t = XML::Twig->new( twig_handlers => { 'WishList/Item' => \&item }, keep_encoding => 1 );
$t->parsefile( ".\\test.xml" );
exit 0;