in reply to XPath search qn

well, i figured out a way to perform tests on more than one child node, but it ain't pretty. i'd stick with attributes.

update: updated the path to suck less. i learned a decent amount about xpath by doing this! :)

#!perl -l use strict; use warnings; use XML::XPath; my $xp = XML::XPath->new("xml" => do { undef $/; <DATA> }); my $path = q{//userprop[nameprop='Version' and valueprop='v1.3']/ances +tor::file}; my $nodes = $xp->find($path); for my $node ($nodes->get_nodelist) { print XML::XPath::XMLParser::as_string($node); } __DATA__ <files> <file> <name>test.doc</name> <userprop> <valueprop>v1.3</valueprop> <nameprop>Version</nameprop> </userprop> </file> <file> <name>test.doc</name> <userprop> <nameprop>Version</nameprop> <valueprop>v1.4</valueprop> </userprop> </file> </files>

perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

Replies are listed 'Best First'.
Re: Re: XPath search qn
by Justudo (Novice) on May 05, 2004 at 08:08 UTC
    I'd stick to attributes too if I had a choice! :) But your code is useful. Thanks. However, what if I want to do something like a wildcard search on 'v1.3' or 'v1.31' etc. ? I tried :
    my $nodes = $xp->find(q{/files/file/userprop/nameprop[text()='Version' +]/../valueprop[text()='v1.3|v1.31']/ancestor::file});
    AND
    my $nodes = $xp->find(q{/files/file/userprop/nameprop[text()='Version' +]/../valueprop[text()='v1.3*']/ancestor::file});
    Both did not work. Is it also possible to do a range search ? Like substr(text(),1,2) > 1.3 to get all version greater than 'v1.3'. Or even a date search ?