perlDevsWorld has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, How can I use XPATH to get the values from the XML. Did some research and installed XML::LibXML

XML file:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <oos:EmpDetailsResponse xmlns:oos="http://www.hrservcices.com/2. +0"> <oos:EmpName id="123">David</oos:EmpName> <oos:EmpName id="1431">George</oos:EmpName> <oos:EmpName id="543">Peter</oos:EmpName> <oos:EmpName id="434">Samuel</oos:EmpName> <oos:EmpName id="4353">Kathy</oos:EmpName> </oos:EmpDetailsResponse> </env:Body> </env:Envelope>

Output I should generate is:

123:David

1431:George

543:Peter

434:Samuel

4353:Kathy

Replies are listed 'Best First'.
Re: Getting the values from XML using XPATH
by tangent (Parson) on Apr 07, 2015 at 21:12 UTC
    This should work:
    for my $emp (@myEmpNodes) { my @names = $emp->findnodes('oos:EmpName'); for my $name (@names) { print "\nEmp: ", $name->getAttribute('id'), "\n"; print $name->textContent, "\n"; } }
      Thanks so much all!.
Re: Getting the values from XML using XPATH
by basiliscos (Pilgrim) on Apr 07, 2015 at 23:14 UTC

    Hi,

    If you are just learning xpath then you can try xacobeo (written in GTK/perl) to figure out how XPath works or to craft your xpath-values.

    WBR, basiliscos.
Re: Getting the values from XML using XPATH
by perlDevsWorld (Novice) on Apr 07, 2015 at 18:15 UTC

    I tried something like this:

    my $context = XML::LibXML::XPathContext->new( $dom->documentElement() + ); my @myEmpNodes = $context->findnodes("//oos:EmpDetailsResponse"); foreach $emp (@myEmpNodes) { print "\nEmp:\n", $emp->findvalue('//oos:EmpName'). "\n";
    This puts everything in same one line and not yet included the ID's though. Appreciate your help in advance
Re: Getting the values from XML using XPATH
by Anonymous Monk on Apr 07, 2015 at 22:35 UTC