Help with XML::Twig xpath syntax, please
- From: Henry Law <news@xxxxxxxxxxxxx>
- Date: Sun, 16 Oct 2005 18:56:10 +0100
I have a simple XML file which contains details of files in a directory (the format is in the sample program below). I want to use XML::Twig (which is in widespread use elsewhere in the program suite) to pull out document elements corresponding to certain criteria, but I cannot for the life of me work out the syntax for the get_xpath method, despite browsing Michel Rodriguez's XMLTwig site and his xpath tester page.
Here's a sample program; what it's intended to do is to pull out the "bkfile" element which has dbid=392; it does that, but pulls out much else besides - including the other bkfile element whose dbid doesn't match - and I can't find out what I'm doing wrong. All help very gratefully received. XML::Twig is a great module and I've used it extensively elsewhere; it would be a nuisance to have to use some other XML module for this task.
This is XML::Twig 3.20 running under ActiveState Perl v5.8.7 and Windows XP (fully patched).
-------------- sample program ------------- use strict; use warnings;
use XML::Twig;
my $xmldoc = new XML::Twig;
my $xmlsource =<<"ENDXML";
<bkdirectory>
<bkfile>
<dbid>393</dbid>
<b_size>177</b_size>
</bkfile>
<bkfile>
<dbid>392</dbid>
<b_size>460</b_size>
</bkfile>
</bkdirectory>
ENDXML$xmldoc->parse($xmlsource);
my @bkfiles = $xmldoc->get_xpath('//bkfile[string(dbid)="392"]]');print "\@bkfiles has ",scalar @bkfiles," elements\n\n";
foreach my $elt (@bkfiles) {
print "----",$elt->name,"\n";
$elt->print;
print "\n";
}
-- output from sample: one line may fold, sorry -------
F:\>tryit.pl
@bkfiles has 11 elements----bkdirectory <bkdirectory><bkfile><dbid>393</dbid><b_size>177</b_size></bkfile><bkfile><dbid>392</dbid><b_size>460</b_size></bkfile></bkdirectory> ----bkfile <bkfile><dbid>393</dbid><b_size>177</b_size></bkfile> ----dbid <dbid>393</dbid> ----#PCDATA 393 ----b_size <b_size>177</b_size> ----#PCDATA 177 ----bkfile <bkfile><dbid>392</dbid><b_size>460</b_size></bkfile> ----dbid <dbid>392</dbid> ----#PCDATA 392 ----b_size <b_size>460</b_size> ----#PCDATA 460
--
Henry Law <>< Manchester, England .
- Follow-Ups:
- Re: Help with XML::Twig xpath syntax, please
- From: Jordi Bernabeu (w00dy)
- Re: Help with XML::Twig xpath syntax, please
- From: Michel Rodriguez
- Re: Help with XML::Twig xpath syntax, please
- Prev by Date: Modules with several packages
- Next by Date: Re: Perl Class::Struct
- Previous by thread: Modules with several packages
- Next by thread: Re: Help with XML::Twig xpath syntax, please
- Index(es):