Question

I need to display the "parent" from the first Element of input.xml

Also I must display the id from the parent

Here is my script Perl: C:\lng> C:\lng\uniq.pl

The output is displaying unfortunately the children nodes:

 <source id="berlin"> 
   <target id="karlsruhe" method="delete"/> 
   <target id="Stuttgart4" method="delete"/>
</source>

The expected output is:

<source id="berlin"> 

Please help.

Here is uniq.pl:

#!/usr/bin/perl -w
use warnings;
use XML::Twig;
my $t= XML::Twig->new;
my $v= XML::Twig::Elt->new;

$t-> parsefile ('input.xml');
$v= $t->first_elt('[@method]');

$w= $v->parent;
$w->print;

The input file: input.xml

<Germany>
 <airport id1="1">
  <plane id="3">
   <source id="berlin">
     <target id="karlsruhe" method="delete"/>
     <target id="Stuttgart4" method="delete"/>
   </source>
  </plane>
 </airport>
</Germany>
Was it helpful?

Solution

When you call print on an element, it prints the element, the entire element, from start tag to end tag, including all children and their descendants.

What you want it just the start tag, using the start_tag method:

print $w->start_tag;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top