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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.