XML::LibXML::XPathContext - Question

Posted by sid_com on Stack Overflow See other posts from Stack Overflow or by sid_com
Published on 2010-04-20T07:36:08Z Indexed on 2010/04/20 7:53 UTC
Read the original article Hit count: 502

Filed under:
|

This script works with and without XPathContext. Why should I use it with XPathContext?

#!/usr/bin/env perl
use warnings; use strict;
use XML::LibXML;
use 5.012;

my $parser = XML::LibXML->new;

my $doc = $parser->parse_string(<<EOT);
<?xml version="1.0"?>
<xml>
    Text im Dokument
    <element id="myID" name="myname" style="old" />
    <object objid="001" objname="Object1" />
    <element id="002" name="myname" />
</xml>
EOT 
#/

# without XPathContext
my $nodes = $doc->findnodes( '/xml/element[@id=002]' );

# with XPathContext
#my $root = $doc->documentElement;
#my $xc = XML::LibXML::XPathContext->new( $root );
#my $nodes = $xc->findnodes( '/xml/element[@id=002]' );


for my $node ( $nodes->get_nodelist ) {
    say "Node: ", $node->nodeName;
    print "Attribute: ";
    print $_->getName, '=', $_->getValue, ' ' for    $node->attributes;
    say "";
}

© Stack Overflow or respective owner

Related posts about perl

Related posts about Xml