English 中文(简体)
How can I access attributes and elements from XML::LibXML in Perl?
原标题:

I am having trouble understanding / using name spaces with XML::LibXML package in Perl. I can access an element successfully but not an attribute. I have the following code which accesses an XML file (http://pastebin.com/f3fb9d1d0).

my $tree = $parser->parse_file($file); # parses the file contents into the new libXML object.
my $xpc = XML::LibXML::XPathContext->new($tree); 
$xpc->registerNs(microplateML =>  http://moleculardevices.com/microplateML );   

I then try and access an element called common-name and an attribute called name.

foreach my $camelid ($xpc->findnodes( //microplateML:species )) {
  my $latin_name = $camelid->findvalue( @name );   
  my $common_name = $camelid->findvalue( common-name );  
  print "$latin_name, $common_name" ;
}

But only the latin-name (@name) is printing out, the common-name is not. What am I doing wrong and how can I get the common-name to print out as well?

What does the @name do in this case? I presume it is an array, and that attributes should be put into an array as there can be more than one, but elements (like common-name) should not be because there should just be one?

I ve been following the examples here: http://www.xml.com/pub/a/2001/11/14/xml-libxml.html and here: http://perl-xml.sourceforge.net/faq/#namespaces_xpath, and trying to get their example camel script working with my namespace, hence the weird namespace.

最佳回答

Make sure you XML file is valid then use $node->getAttribute("someAttribute") to access attributes.

@name is a attribute name. You d use it in findnodes() to specify elements with a given attribute set. Eg. a path like:

//camelids/species[@name="Camelus bactrianus"]/

Here is a simple/contrived example:

#!/usr/bin/perl -w
use XML::LibXML;

my $parser = XML::LibXML->new();
my $doc = $parser->parse_file( /Users/castle/Desktop/animal.xml );

my $xc = XML::LibXML::XPathContext->new( $doc->documentElement()  );
$xc->registerNs( ns ,  http://moleculardevices.com/microplateML );

my @n = $xc->findnodes( //ns:species );
foreach $nod (@n) {
    print "A: ".$nod->getAttribute("name")."
";

    my @c = $xc->findnodes("./ns:common-name", $nod);
    foreach $cod (@c) {
        print "B: ".$cod->nodeName;
        print " = ";
        print $cod->getFirstChild()->getData()."
";
    }
}

Output is:

perl ./xmltest.pl 
A: Camelus bactrianus
B: common-name = Bactrian Camel
问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签