Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:48:10+00:00 2026-05-13T09:48:10+00:00

I am having trouble understanding / using name spaces with XML::LibXML package in Perl.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:48:10+00:00Added an answer on May 13, 2026 at 9:48 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 292k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Wikipdedia has the info you want Fields with embedded commas… May 13, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer You can use the checkbox formatter to display a cell… May 13, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer The user-agent in the HTTP request can help detect the… May 13, 2026 at 6:12 pm

Related Questions

I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
I'm having trouble with section 4 of the stanford iphone class on assignment 1b.
I have written a set of classes and interfaces that are implemented in Moose
I am having trouble understanding how the System Registry can help me convert a
I am having trouble understanding a question. The question asks first to write a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.