I want to find the 'Instruction' element where 'Type'='Motor'.

->first_child('Instruction[@Type="Motor"]')

#!/usr/bin/perl use strict; use warnings; use XML::Twig; # build a new file in the output format. my $Output_file_name = 'testOutput.xml'; my $outRoot = XML::Twig::Elt->new('Motors'); #process input XML data my $twig = new XML::Twig( twig_roots=>{ Sheet=>\&motors } ); $twig->parse(*DATA); #output results $outRoot->set_pretty_print('indented'); $outRoot->print;#_to_file($Output_file_name); sub motors{ my ($twig, $elt)=@_; my $motor = $elt->first_child('Instruction[@Type="Motor"]'); my $motorID = $motor->att('ID'); my $newRoot = $outRoot->insert_new_elt( 'Motor'=>{ Name=> $motor->att('Name'), ID => $motorID } ); my %InOut=(); for ( $elt->children( 'Input|Output' ) ){ $InOut{'Motor_'.$_->gi}[ $_->att('ID') ] = $_->att('Text'); } my $xpath='Wire[@ToID="'.$motorID.'" or @FromID="'.$motorID.'"]'; for my $c ( $elt->children( $xpath ) ){ my ($id,$text); if ( $c->att('ToID' ) eq $motorID ){ $id = $c->att( 'FromID' ); $text = $c->att( 'ToText' ); } else { $id = $c->att( 'ToID' ); $text = $c->att( 'FromText' ); } $newRoot->insert_new_elt('last_child', $text, $InOut{$text}[$id] ) }; }
poj

In reply to Re: XML::Twig efficiency with attributes by poj
in thread XML::Twig efficiency with attributes by timmmy

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.