How to make Perl XML::XPath allow queries without namespace prefixes? -
i'm trying use xml::xpath extract content xml documents. documents specified namespaces, want use xpath expressions without namespaces. far can tell, had working fine in 2 different scripts.
it seems sometime today, behavior of xml::xpath has changed respect this. don't see have changed has made behavior change.
i can manual tests work, if specify namespaces, using call "set_namespace()" in script (hardcoding prefix expect use) , specifying prefix in xpath expression.
again, i'm pretty sure had working yesterday, without calling "set_namespace()" in script, or specifying prefixes in xpath expressions.
if don't add "set_namespace()" call , specify prefixes in expression, empty nodesets queries.
i tried setting "$xml::xpath::namespaces" 0 before create first xpath object, doesn't seem make difference.
the following simple script pipe xml into:
#! /bin/perl use xml::xpath; use xml::xpath::xmlparser; use getopt::long; $| = 1; $opt_file; getoptions("f|file=s" => \$opt_file); $xml::xpath::namespaces = 0; $xpath; if ($opt_file ne '') { $xpath = xml::xpath->new(filename => $opt_file); } else { $xpath = xml::xpath->new(ioref => \*stdin); } while (my $expr = shift @argv) { $nodeset = $xpath->find($expr); if ($nodeset) { if ($opt_file ne '') { print $opt_file . ":\n"; } $node; $node ($nodeset->get_nodelist) { print $node->string_value() . "\n"; } } }
here's sample command line:
% echo "<ns3:abc xmlns:ns3=\"xxx\"><ns3:def>ghi</ns3:def></ns3:abc>" | xpathtext "//def"
i hope "ghi" this, i'm getting nothing.
wow, module buggy.
let's forget question minute , use $xml::xpath::namespaces=1;
(the default) now.
$ perl -e'say q{<r><e>e</e></r>}' | xpathtext //e e
correct. there
e
element in null namespace.$ perl -e'say q{<r xmlns:p="http://n"><p:e>e</p:e></r>}' | xpathtext //e [nothing]
correct. there no
e
elements in null namespace.$ perl -e'say q{<r xmlns="http://n"><e>e</e></r>}' | xpathtext //e e
incorrect. there no
e
elements in null namespace, 1 printed.$ perl -e'say q{<r><e xmlns="http://n">e</e></r>}' | xpathtext //e e
incorrect. there no
e
elements in null namespace, 1 printed.$ perl -e'say q{<r xmlns:p="http://n"><p:e>e</p:e></r>}' | xpathtext //p:e e
incorrect. should error there's no way of knowing whether
p
in xpath refershttp://n
namespace or not.$ perl -e'say q{<r xmlns="http://n"><e>e</e></r>}' | xpathtext //p:e [nothing]
incorrect. should error there's no way of knowing whether
p
in xpath refershttp://n
namespace or not.
given level of bugginess, it's no surprise you're having problems.
now let's find out $xml::xpath::namespace=0;
does.
after rerunning above programs $xml::xpath::namespaces=0;
, find answer "absolutely nothing".
i've confirmed attaching magic variable. variable never used (in latest version, xml-xpath-1.13)!
so module half want, , half should no apparent means of customising it.
Comments
Post a Comment