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

  • SEARCH
  • Home
  • 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 8411969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:33:35+00:00 2026-06-10T00:33:35+00:00

I am coding with Perl on a Window 7 machine. I am able to

  • 0

I am coding with Perl on a Window 7 machine. I am able to extract data from the XML using the XPath code below

use strict;
use warning;

use XML::LibXML;

my $parser = XML::LibXML->new();
        my $doc    = $parser->parse_file($newfile);
        my $query  = "/tradenet/message/header/unique_ref_no/date/text( )";
        my($node)   = $doc->findnodes($query);
        $node->setData("$file_seq_number");  

However, when i use the same code on a different XML, the xpath from the second document looks as below:

/TradenetResponse/OutboundMessage/out:OutwardPermit/out:Declaration/out:Header/cac:UniqueReferenceNumber/cbc:SequenceNumeric

Together with the Perl code, this is what the extraction code looks like:

my $parser = XML::LibXML->new();
    my $doc    = $parser->parse_file($newfile);
    my $query  = "/TradenetResponse/OutboundMessage/out:OutwardPermit/out:Declaration/out:Header/cac:UniqueReferenceNumber/cbc:SequenceNumeric/text( )";
    my($node)   = $doc->findnodes($query);
    $node->setData("$file_seq_number");

Using the second code, I am unable to retrieve the data from the second XML. I receive this error “Can’t call method “setData”on an undefined value at Perl.pl line 5”.

Does the “:” character in the second XPATH address affecting the code?

  • 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-06-10T00:33:36+00:00Added an answer on June 10, 2026 at 12:33 am

    You have to define what out, cac, and cbc mean in order for the XPath query to find the appropriate nodes:

    my $doc = $parser->parse_file($newfile);
    my $xpath_context = XML::LibXML::XPathContext->new($doc->documentElement());
    
    # These URIs need to be the same as the ones in the source document
    $xpath_context->registerNs('out', 'http://example.com/out.xsd');
    $xpath_context->registerNs('cac', 'http://example.com/cac.xsd');
    $xpath_context->registerNs('cbc', 'http://example.com/cbc.xsd');
    
    my $query  = "/TradenetResponse/OutboundMessage/out:OutwardPermit/out:Declaration/out:Header/cac:UniqueReferenceNumber/cbc:SequenceNumeric/text( )";
    my ($node) = $xpath_context->findnodes($query);
    

    As promised, here is a working example. First, the test input file:

    <?xml version="1.0"?>
    
    <!-- input.xml -->
    
    <TradenetResponse xmlns:a="http://example.com/out.xsd"
                      xmlns:b="http://example.com/cac.xsd"
                      xmlns:c="http://example.com/cbc.xsd">
      <OutboundMessage>
        <a:OutwardPermit>
          <a:Declaration>
            <a:Header>
              <b:UniqueReferenceNumber>
                <c:SequenceNumeric>1234</c:SequenceNumeric>
              </b:UniqueReferenceNumber>
            </a:Header>
          </a:Declaration>
        </a:OutwardPermit>
      </OutboundMessage>
    </TradenetResponse>
    

    And here is the working Perl script:

    #!/usr/bin/perl
    
    # parse.pl
    
    use strict;
    use warnings;
    use XML::LibXML;
    
    my $parser = XML::LibXML->new();
    
    my $newfile = "input.xml";
    my $doc = $parser->parse_file($newfile);
    my $xpath_context = XML::LibXML::XPathContext->new($doc->documentElement());
    
    # These URIs need to be the same as the ones in the source document
    $xpath_context->registerNs('out', 'http://example.com/out.xsd');
    $xpath_context->registerNs('cac', 'http://example.com/cac.xsd');
    $xpath_context->registerNs('cbc', 'http://example.com/cbc.xsd');
    
    # Query wrapped for clarity                                                                                                         
    my $query = "/TradenetResponse/OutboundMessage/out:OutwardPermit" .
                "/out:Declaration/out:Header/cac:UniqueReferenceNumber" .
                "/cbc:SequenceNumeric/text()";
    
    my ($node) = $xpath_context->findnodes($query);
    
    print "Value: " . $node->getData() . "\n";
    

    The output for me is:

    sean@localhost:~xmltest$ ./parse.pl
    Value: 1234
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am inserting data from perl in my sqlite database. here is my coding:
From everything I've read on using Perl modules, the basic usage is: Module file
I find that using labels inside Perl subroutines, to break from multiple loops, or
My day job involves coding with Perl. At home I play around with Python
O.K., I'm coming from Perl to Python and I don't have much experience in
I am new to Perl coding. I am facing a problem while executing a
I am a web developer well-versed in XHTML/CSS, JavaScript, Perl, PHP, and XML/XSL. I
I am coding in Perl and i'm having some minor issue over here. I
Alrighty, coding in Perl and just had a quick question. I have class created
I've just started coding in Perl and am just looking to find out if

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.