Thanks toolic! I went with your modification and extended it a bit to include a couple more conditions.

use warnings; use strict; use XML::Twig; my $source_file = 'datafile_towns.xml'; my $out_file = 'illytowns.xml'; my $twig = XML::Twig->new( twig_handlers => { town => \&town }, ); $twig->parsefile($source_file); open (my $fh_out, '>', $out_file) or die "unable to open '$out_file' f +or writing: $!"; sub town { my ($twig, $town) = @_; my $townx = $town->first_child('location')->first_child('mapx')->t +ext; my $towny = $town->first_child('location')->first_child('mapy')->t +ext; if (($townx > 556) and ($towny > -2502) and ($towny < -2214)) { print $town->first_child('towndata')->first_child_text('townna +me'); print "\n"; } $twig->print($fh_out); $twig->purge; }
I've added a file handle to print the data to a file rather than to STOUT; however, it is not working correctly, as instead of getting a file with a list of town names I get a a full set of tags for 1 XML element (probably the last element in the XML file). The STDOUT generated by the print statement inside the twig is correct.

In reply to Re^2: TWIGging XML to CSV: what's wrong with my syntax? by tinker
in thread TWIGging XML to CSV: what's wrong with my syntax? by tinker

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.