Help with XML::Twig xpath syntax, please

Help with XML::Twig xpath syntax, please

am 16.10.2005 19:56:10 von Henry Law

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";


393
177


392
460


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
393177392460
----bkfile
393177
----dbid
393
----#PCDATA
393
----b_size
177
----#PCDATA
177
----bkfile
392460
----dbid
392
----#PCDATA
392
----b_size
460
----#PCDATA
460

--

Henry Law <>< Manchester, England

Re: Help with XML::Twig xpath syntax, please

am 17.10.2005 15:15:38 von Michel Rodriguez

Henry Law wrote:
> 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.
>
> my @bkfiles = $xmldoc->get_xpath('//bkfile[string(dbid)="392"]]');

Oops! You found the edge of XML::Twig's rudimentary XPath support. It
should really throw an error and admint that it can't deal with this
type of query. I will fix this (by throwing said error),

If you have XML::XPath installed, or if you can install it on that
machine, then you can use XML::TwigXPath instead of XML::Twig, and then
you get full XPath support through the findnodes method.

Let me know if this helps.

--
mirod

Re: Help with XML::Twig xpath syntax, please

am 17.10.2005 19:57:36 von jorge.bernabeu

Henry Law wrote:
> [...]
> 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";
>
>
> 393
> 177
>

>
> 392
> 460
>

>

> 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";
> }

Hi,

Have you tried using TwigHandlers when creating $xmldoc?

With get_xpath() you get the whole parent-tree (up to the root, hence
you get the whole document).

Try putting your xpath in TwigHandlers and your processing in a sub,
like this:

--------------------------- sample program -------------------
#!/usr/bin/perl
use strict;
use warnings;

use XML::Twig;

my $xmlsource =<<"ENDXML";


393
177


392
460


ENDXML

my $xmldoc = new XML::Twig( TwigHandlers =>
{
'bkfile[string(dbid)="392"] => \&process
}
);
$xmldoc->parse($xmlsource);

sub process
{
my ($twig, $element) = @_;
print $element->name,"\n";
foreach my $child ($element->children)
{
print $child->name,": ",$child->text,"\n";
}
}
--------------------- /sample program ----------------------------

Hope this helps.

Jordi (w00dy)

Re: Help with XML::Twig xpath syntax, please

am 17.10.2005 20:32:59 von Henry Law

Michel Rodriguez wrote:

> Oops! You found the edge of XML::Twig's rudimentary XPath support. It
> should really throw an error and admint that it can't deal with this
> type of query. I will fix this (by throwing said error),
>
> If you have XML::XPath installed, or if you can install it on that
> machine, then you can use XML::TwigXPath instead of XML::Twig, and then
> you get full XPath support through the findnodes method.
>
> Let me know if this helps.

Done that and it does exactly what I want. Wasn't hard to install the
additional module either. Thank you.

--

Henry Law <>< Manchester, England