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 3669754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:16:53+00:00 2026-05-19T02:16:53+00:00

So I have some perl code that goes something like: use strict; use XML::XPath;

  • 0

So I have some perl code that goes something like:

use strict;
use XML::XPath;

# $xml is an xml string read from file
my $returnVal = mySubA($xml);

#... some more code ...

# sub definition
sub mySubA {
  my ($xml) = @_;
  my $xp = XML::XPath->new(xml => $xml);

  my @fields = $xp->find('/elA/elB/elC')->get_nodelist;

  # foreach elC, get desired info...
  foreach my $field (@fields) {
    $xp = XML:XPath->new(context => $field);
    my $info1 = $xp->find('/info1')->string_value;
    print "\nINFO1: $info1";

    my $info2 = $xp->find('/info2')->string_value;
  }
  # do some stuff....
  return $returnVal;
}

This works fine and prints out INFO1: value… . I’m now changing the code, and am trying to get information from another section of the xml, and so I’ve written a different sub for it. To avoid parsing the same xml twice, I’m trying to write my code only using 1 xpath object (see XPath new in perl docs).

So now my code is something like….

use strict;
use XML::XPath;

# $xml is an xml string read from file
my $xp = XML::XPath->new(xml => $xml);
my $returnValA = mySubA($xp);
my $returnValB = mySubB($xp); 

#... some more code ...

# sub definition
sub mySubA {
  my ($xp) = @_;

  my @fields = $xp->find('/elA/elB/elC')->get_nodelist;

  # foreach elC, get desired info...
  foreach my $field (@fields) {
    $xp = XML:XPath->new(context => $field);
    my $info1 = $xp->find('/info1')->string_value;
    print "\nINFO1: $info1";

    my $info2 = $xp->find('/info2')->string_value;
  }
  # do some stuff...
  return $returnVal;
}

sub mySubB {
  my ($xp) = @_;
  # do some stuff....
  return $returnVal;
}

So, basically, I’m passing the ref to the xpath object into mySubA instead of creating it within mySubA. The problem is that no values are being found, even though I’m pretty sure the xpath expression is resolving something because the loop iterates about 10 times. I’m new to perl, so I might be missing something here, but the thing that is confusing for me is that the second method – mySubB – is working fine; kind of blowing away my first suspicion that there is a problem with passing the xpath object into the sub (as in, I’m not dereferencing/referencing the xpath object as I should be).

I don’t know if its relevant or not, but the xml I’m working with doesn’t contain any namespaces or attributes.

  • 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-19T02:16:54+00:00Added an answer on May 19, 2026 at 2:16 am

    Unless you are going to extract more than just one child element, I would use a direct XPath expression:

    /elA/elB/elC/info1
    

    If this is not your case, then you should use the XPath expression to select the parents elC and after that use simple DOM method to get the children.

    If you are going to use some complex selecting from the elC element, then there should be for you in your XPath engine some method taking an expression and a context node. But do note that if elC is the context node, then you should use a relative XPath like info1 instead of an absolute XPath like /info1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Perl code that translates new-lines and line-feeds to a normalized form.
I have some Perl code that executes a shell script for multiple parameters, to
I have some encryption code that has been written in Perl (also a code
I currently use Strawberry Perl as my primary Perl distribution. However, I have some
I have a little Perl script (On Windows) that checks some files for me
I often have a subroutine in Perl that fills an array with some information.
I have some Perl code (for performance analysis) first developed under Linux which now
I have a Perl script that gives me a 50 character string of random
I have some questions about Perl's map function. Specifically: How does %hash = map
I have some issue with a Perl script. It modifies the content of a

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.