How can I access attributes and elements from XML::LibXML in Perl?

John   picture John · Jan 11, 2010 · Viewed 14.3k times · Source

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.

Answer

Niels Castle picture Niels Castle · Jan 11, 2010

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")."\n";

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

Output is:

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