Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

help me with perl script that add xml attritutes

by veerubiji (Sexton)
on Nov 04, 2011 at 15:08 UTC ( #935956=perlquestion: print w/ replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, may be this seems to simple but for me its hard because I am new to perl I have one xml file like this

<university> <name>svu</name> <location>ravru</location> <branch> <electronics> <student name="vijju" number="12"> <semester number="1"subjects="7" rank="2"/> </student> <student name="mikky" number="15"> <semester number="1" subjects="7" rank="10"/> <semester number="2" subjects="4" rank="1"/> </student> <student name="clar" number="16"> <semester number="1"subjects="7" rank="2"/> <semester number="3"subjects="4" rank="2"/> </student> </electronics> </branch> </university> . . . . . <data> <student name="vijju" number="12" address="polugaripalli,t.v.puram, ba +dvel, kadapa"/> <student name="mikky" number="15" address="papireddypalli, badvel, kad +apa"/> <student name="clar" number="16" address="naidu nagr, badvel, kadapa"/ +> <student name="xxx" number="17" address="gandhi nagr, badvel, kadapa"/ +> <student name="xxx" number="12" address="gudur, badvel, kadapa"/> . . . . <semister number="1" subject names="EDC,PDC,CDS"/> <semister number="2" subject names="M1,LICA,SS"/> <semister number="3" subject names="DIP,DSP,CEM"/> .... .. .. </data>

I need to add address from attribute studet in data element to the student attribut in the electronics element in the university tag, I need to add how many times I found the same name and number. same like subject names from data element to semister attribute in the electronics element in university tag. my output need to be like this.

<university> <name>svu</name> <location>ravru</location> <branch> <electronics> <student name="vijju" number="12" address="polugaripalli,t.v.pura +m, badvel, kadapa"> <semester number="1"subjects="7" rank="2" subject names="EDC,PDC, +CDS" /> </student> <student name="mikky" number="15" address="papireddypalli, badvel +, kadapa" > <semester number="1" subjects="7" rank="10" subject names="EDC,PD +C,CDS"/> <semester number="2" subjects="4" rank="1" subject names="M1,LICA +,SS"/> </student> <student name="clar" number="16" address="naidu nagr, badvel, k +adapa" > <semester number="1"subjects="7" rank="2"subject names="EDC,PDC +,CDS" /> <semester number="3"subjects="4" rank="2" subject names="DIP,DSP +,CEM" /> </student> </electronics> </branch> </university>

Comment on help me with perl script that add xml attritutes
Select or Download Code
Re: help me with perl script that add xml attritutes
by moritz (Cardinal) on Nov 04, 2011 at 15:13 UTC
Re: help me with perl script that add xml attritutes
by choroba (Canon) on Nov 04, 2011 at 22:14 UTC
    I usually use XML::XSH2 for XML manipulation. In this case, I needed to wrap your input file into <xml>...</xml> and to add _ between subject and names to make it a valid XML. The output is also different to your expected output: spaces between attributes are not optional in XML.
    my $input := open 935956.xml ; my $student_hash := hash concat(@name, '#', @number) $input/xml/data/s +tudent ; my $semister_hash := hash ../@number $input/xml/data/semister/@subject +_names ; my $out := create xml ; for my $university in $input/xml/university { my $new_uni := cp $university into $out/xml ; for my $student in $new_uni//student { cp xsh:lookup('student_hash', concat($student/@name, '#', $stu +dent/@number))/@address into $student ; for $student/semester cp xsh:lookup('semister_hash', ./@number +) into . ; } } save :f 935956-out.xml $out ;
      thanks its working

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://935956]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2015-03-31 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:









    Results (672 votes), past polls