TX for your reply. I have implemented some codes but I have problem for replace the REF element with EXT element. This is the original file
<DOC id="AFP_ENG_20050316.0102" type="story"> <HEADLINE> Bobby Fischer can escape US if Iceland makes him citizen: Japanese lawmaker by Hiroshi Hiyama ATTENTION - ADDS quotes from immigration official, details /// </HEADLINE> <DATELINE> TOKYO, March 16 </DATELINE> <TEXT> <S Entail="28" s_id="0"> <REF YPE="PROPNAME">Bobby Fischer</REF>can escape US if Iceland makes +<REF ANT-ID="100" EXT="Bobby Fischer" ID="101">him</REF> citizen: Japanese lawmaker by Hiroshi Hiyama ATTENTION - ADDS quotes from immigration official, details /// </S><S Entail="28-31" s_id="1"> Chess legend <REF ID="102" YPE="PROPNAME">Bobby Fischer</REF>, who fa +ces prison if <REF ANT-ID="102" EXT="Bobby Fischer" ID="103" YPE="PRO +N">he</REF> returns to the United States, can only avoid deportation +from Japan if <REF ID="104" YPE="PROPNAME">Iceland</REF> upgrades <REF ANT-ID="104" + EXT="Iceland's" ID="105">its</REF> granting of residency to full cit +izenship, a Japanese lawmaker said Wednesday. </S> </TEXT> </DOC>
and my expected file is mentioned as below .some of the REF tags do not have EXT ,for that cases there is not replacment
<DOC id="AFP_ENG_20050316.0102" type="story"> <HEADLINE> Bobby Fischer can escape US if Iceland makes him citizen: Japanese lawmaker by Hiroshi Hiyama ATTENTION - ADDS quotes from immigration official, details /// </HEADLINE> <DATELINE> TOKYO, March 16 </DATELINE> <TEXT> <S Entail="28" s_id="0"> <REF ID="100" YPE="PROPNAME">Bobby Fischer</REF>can escape US if Icela +nd makes <REF ANT-ID="100" EXT="Bobby Fischer" ID="101">Bobby Fische +r</REF> citizen: Japanese lawmaker by Hiroshi Hiyama ATTENTION - ADDS quotes from immigration official, details /// </S><S Entail="28-31" s_id="1"> Chess legend <REF ID="102" YPE="PROPNAME">Bobby Fischer</REF>, who fac +es prison if <REF ANT-ID="102" EXT="Bobby Fischer" ID="103" YPE="PRO +N">Bobby Fischer</REF> returns to the United States, can only avoid d +eportation from Japan if <REF ID="104" >Iceland</REF> upgrades <REF ANT-ID="104" EXT="Iceland' +s" ID="105">Iceland's</REF> granting of residency to full citizenship +, a Japanese lawmaker said Wednesday. </S> </TEXT> </DOC>
my code is
#!/bin/perl -w use strict; use XML::Twig; my $twig=new XML::Twig(twig_roots => {'TEXT' =>1} ,twig_handlers=>{'R +EF' => \&REF}); # change the address of root to TEXT element because +REF is the children of TEXT my $field='REF'; $twig->parsefile("AFP_ENG_20050316.0102.xml"); #Parse the file my $root=$twig->root; my $rootchild=$root->children; sub REF { my ($twig ,$field)=@_; my $extension = $field->text;#keep the value of REF element my $att=$field->att('EXT'); #keep the value of Ext ( some REF elemen +ts do not have EXT attribute) print "$extension\n"; $att->set_text($field); $extension=$att; } $twig->print;
I would like to know is there function in twig to remove the REF elements after this replacement but keep the value of them in sentence? thanks a lot.
In reply to Re^4: Search and Replace in XML
by Anonymous Monk
in thread Search and Replace in XML
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |