简体   繁体   中英

XML::LibXML: doc - root

Is the here the call of documentElemtent in the first example superfluous?

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

my $file = 'xml_file';

my $doc = XML::LibXML->load_xml( location => $file );
my $root = $doc->documentElement();
my $xpc = XML::LibXML::XPathContext->new( $root );
# ...
say $_->nodeName for $xpc->findnodes( '/' );

outputs

#document

$doc = XML::LibXML->load_xml( location => $file );
$xpc = XML::LibXML::XPathContext->new( $doc );
# ...
say $_->nodeName for $xpc->findnodes( '/' );

outputs also

#document

Any prefixes defined in the topic node are adopted by the xpc, so the two are different if there are prefixes defined on the root node.

use warnings;
use strict;
use feature qw( say );

use XML::LibXML qw( );

my $xml = <<'__EOI__';
<root xmlns:foo="uri:xxx">
<foo:bar/>
</root>
__EOI__

my $doc  = XML::LibXML->load_xml( string => $xml );
my $root = $doc->documentElement();

{
   my $xpc = XML::LibXML::XPathContext->new($doc);
   say "doc:";
   say $_->nodeName for $xpc->findnodes('foo:bar');
}

say "";

{
   my $xpc  = XML::LibXML::XPathContext->new($root);
   say "root:";
   say $_->nodeName for $xpc->findnodes('foo:bar');
}

doc:

root:
foo:bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM