How to make XML::XPath ignore namespaces?

Discussion in 'Perl Misc' started by David Karr, May 21, 2013.

  1. David Karr

    David Karr Guest

    I'm trying to use XML::XPath to extract content from XML documents. The documents are specified with namespaces, but I want to use XPath expressions without namespaces. As far as I can tell, I had this working perfectly fine in two different scripts.

    It seems like sometime today, the behavior of XML::XPath has changed with respect to this. I don't see what I could have changed that has made this behavior change.

    I can get some manual tests to work, if I almost fully specify namespaces, using a call to "set_namespace()" in the script (hardcoding the prefix I expect to use) and specifying the prefix in the XPath expression.

    Again, I'm pretty sure I had this working yesterday, without calling "set_namespace()" in the script, or specifying prefixes in the XPath expressions.

    If I don't add that "set_namespace()" call and specify prefixes in the expression, I just get empty nodesets from my queries.

    I tried setting "$XML::XPath::Namespaces" to zero before I create the first XPath object, but that doesn't seem to make any difference.

    The following is a simple script I pipe XML into:
    --------------------
    #! /bin/perl
    use XML::XPath;
    use XML::XPath::XMLParser;
    use Getopt::Long;

    $| = 1;

    my $opt_file;
    GetOptions("f|file=s" => \$opt_file);

    $XML::XPath::Namespaces = 0;

    my $xpath;
    if ($opt_file ne '') {
    $xpath = XML::XPath->new(filename => $opt_file);
    }
    else {
    $xpath = XML::XPath->new(ioref => \*STDIN);
    }

    while (my $expr = shift @ARGV) {
    my $nodeset = $xpath->find($expr);
    if ($nodeset) {
    if ($opt_file ne '') {
    print $opt_file . ":\n";
    }
    my $node;
    for $node ($nodeset->get_nodelist) {
    print $node->string_value() . "\n";
    }
    }
    }
    ------------------

    Here's a sample command line:

    % echo "<ns3:abc xmlns:ns3=\"xxx\"><ns3:def>ghi</ns3:def></ns3:abc>" | xpathtext "//def"

    I would hope to get "ghi" from this, but I'm currently getting nothing.
    David Karr, May 21, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,964
    jan V
    Jul 29, 2005
  2. Jim
    Replies:
    2
    Views:
    447
    Richard Tobin
    Jan 29, 2004
  3. kj
    Replies:
    3
    Views:
    22,574
    sandhya
    Mar 27, 2010
  4. Rob Meade
    Replies:
    6
    Views:
    264
    Rob Meade
    Mar 1, 2004
  5. Andreas Leitgeb
    Replies:
    8
    Views:
    1,132
    Andreas Leitgeb
    Jul 13, 2012
Loading...

Share This Page