perl - Parent element does not close after deleting an element: XML Twig -


i'm trying find element attribute value , removing document.

in example i'm looking file element, attribute called "relativepath" value "....\ts\etestscenario.inc".

the problem: last element - </files> missing after run script. also, 'visualstudioproject' closing tag printed twice. not sure im doing worng here.

use strict; use xml::twig; $filename = 'z:\autotest\test.xml'; $t= new xml::twig( twigroots=>              { files => \&upd_files_section              },                twig_print_outside_roots => 1,               # print rest               keep_spaces => 1,               keep_atts_order=>1,             );                $t->parsefile("$filename"); #$t->parsefile_inplace ("$filename");   sub upd_files_section{     ($t, $inputfields)=@_;      $file_to_delete = $inputfields->get_xpath("./file[\@att='..\..\ts\etestscenario.inc']");     $inputfields->delete($file_to_delete);     $t->flush; } 

incorrect xml output:

<visualstudioproject     projecttype="visual c++"     version="8.00"     name="xxx"     projectguid="{}"     rootnamespace="xx"     sccprojectname="x"     scclocalpath="."     sccprovider="x"     >         <files>                 <filter name="source files" filter="cpp;bat">                         <file relativepath="..\..\ts\adk_maccommon_test.cpp">                         </file>                         <file relativepath="..\..\fso\eadk.cpp">                         </file>                 <filter filter="ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" name="resource files">                 </filter>                 <file relativepath="..\..\ts\etestscenario.inc">                 </file>         </visualstudioproject>         <globals>         </globals> </visualstudioproject> 

as mentioned, xml not valid, , code doesn't compile.

that said if understand correctly, think handler should be:

sub upd_files_section{     ($t, $inputfields)=@_;     @files_to_delete = $inputfields->get_xpath("./file[\@relativepath='..\\..\\ts\\etestscenario.inc']");     foreach $file (@files_to_delete) { $file->delete; }     $t->flush; } 

a few notes:

  • you need escape backslashes in xpath expression, or perl thinks they're there escape following character; alternatively, use simple quotes or q{} around expression: q{./file[@relativepath='..\..\ts\etestscenario.inc']}
  • in xpath expression, should @relativepath, not @att
  • get_xpath returns list of elements, have delete
  • to delete element call delete on it, no need go through parent dom
  • you cut children @ once using the... cut_children method: $inputfields->cut_children( q{file[@relativepath='..\..\ts\etestscenario.inc']});

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -