Take the 2-minute tour ×

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"/>

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;

The input file: input.xml

 <airport id1="1">
  <plane id="3">
   <source id="berlin">
share|improve this question

1 Answer 1

up vote 5 down vote accepted

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;
share|improve this answer
superb, exactly what I needed. Thanks so much for the fast reply. But if I had to find this method described over the internet, is there an URL where I can find those useful methods (start_tag) ? –  laurentngu Jun 17 '12 at 20:25
xmltwig.org/xmltwig has all the docs, but to be honest start_tag is not one that's often used, if you want the tag name and the id, usually you would use tag and id, by using start_tag you risk getting other attributes of the element. You can also (on *nix) do man XML::Twig or perldoc XML::Twig –  mirod Jun 18 '12 at 7:46
thank you for those details. By using start_tag, I get exactly what I needed –  laurentngu Jun 18 '12 at 8:26

Your Answer


