snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:

Sorry for mocking the details first ,please refer below :: I have xml file as below :
<application_list> <application id = "111"> <contacts> <technical> <name>John</name> <name>Ram</name> <name>Rahim</name> </technical> </contacts> </application> <application_list>
Output required as : Jhon,Ram,Rahim code I tired ...
#! /usr/bin/perl use warnings; use strict; use XML::XPath; my $appxml = 'ApplicationList.xml'; my $appxp = XML::XPath->new(filename => $appxml); my $xp = $appxp->findnodes('//application_list/application/'); my %appid; my @apprecords; foreach my $appnodeset ($xp->get_nodelist) { my $id = $appnodeset->find('@id')->string_value; $id =~ s/^\s+|\s+$//g; foreach my $spltname ($appxp->findnodes('./contacts/technical',$ap +pnodeset)) { my $name = join ",", map $_->string_value, $spltname->findno +des('.//name1'); # add this as suggested by choroba $appid{$id} = $name; print $name; } }
I tired above code but not getting comma separated values in o/p : JhonRamRahim

Replies are listed 'Best First'.
Re: get common node of xml in comma separated string
by choroba (Cardinal) on Aug 02, 2017 at 12:24 UTC
    Several issues.
    1. The XML isn't well-formed.
    2. The code doesn't compile under strict.
    3. What's $xp?
    4. You can't call the find method on a nodelist.
    5. You can't call a method on an undefined value, i.e. when you create a new variable with my but don't assign anything to it, it's wrong to call ->find on the next line.
    Here's my approach, as close to yours as possible:
    #! /usr/bin/perl use warnings; use strict; use XML::XPath; my $appxp = XML::XPath->new(filename => shift); my $xp = $appxp->findnodes('//application/'); my %appid; my @apprecords; for my $node ($xp->get_nodelist) { my $id = $node->find('@id')->string_value; $id =~ s/^\s+|\s+$//g; for my $spltnum ($node->findnodes('number/mobile/',$node)) { my $contact = join ",", map $_->string_value, $spltnum->findno +des('.//contact'); $appid{$id} = $contact; } }
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: get common node of xml in comma separated string
by 1nickt (Canon) on Aug 02, 2017 at 12:23 UTC

    What is $xp ? This code will not work. Where is use strict; ? Why do you not make an SSCCE with your sample data in the __DATA__ section ? You know that this is what is requested by the Monks who are willing to help you learn. Please provide code that will run.


    The way forward always starts with a minimal test.
Re: get common node of xml in comma separated string
by thanos1983 (Parson) on Aug 02, 2017 at 15:00 UTC

    Hello snehit.ar,

    I run your updated code and sample of data and I got:

    $ perl test.pl no element found at line 12, column 0, byte 209: </application> <application_list> ^ at /home/tinyos/perl5/lib/perl5/x86_64-linux/XML/Parser.pm line 187.

    Simply modify your xml file to:

    <application_list> <application id = "111"> <contacts> <technical> <name>John</name> <name>Ram</name> <name>Rahim</name> </technical> </contacts> </application> </application_list>

    And re run your script:

    #!/usr/bin/perl use strict; use warnings; use XML::XPath; use feature 'say'; my $appxml = 'ApplicationList.xml'; my $appxp = XML::XPath->new(filename => $appxml); my $xp = $appxp->findnodes('//application_list/application/'); my %appid; my @apprecords; foreach my $appnodeset ($xp->get_nodelist) { my $id = $appnodeset->find('@id')->string_value; $id =~ s/^\s+|\s+$//g; foreach my $spltname ( $appxp->findnodes('./contacts/technical', $appnodeset) ) { my $name = join ",", map $_->string_value, $spltname->findnodes('.//name'); # add this as suggested by chorob +a $appid{$id} = $name; say $name; } } __END__ $ perl test.pl John,Ram,Rahim

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Thank you!