OK, here is my take (using the latest CPAN version of XML::Twig). The only difficult thing here is how to match elements which do _not_ have the vob attribute. I have to look at the XPath spec (Matt?) to figure out how to express it... and then add it to the module. In the meantime I use a coderef to get it (to compute @vob_not I use an anonymous sub that returns true if the element does not have a vob attribute).

#!/bin/perl -w use strict; use XML::Twig; my $twig= new XML::Twig; $twig->parse( \*DATA); # build the twig my $root= $twig->root; # get the root of the twig (stats) my @vob_not = $root->children( sub { my $elt= shift; !$elt->att( 'v +ob')}); my @vob_none = $root->children( q{player[@vob="none"]} ); my @vob_letter = $root->children( q{player[@vob=~/^[a-z]$/]}); @vob_letter = sort { $a->att( 'vob') cmp $b->att( 'vob') } @vob_letter; my %letters= map { $_->att( 'vob') => 1 } @vob_letter; print "No attribute or vob=none\n", "-" x 20, "\n"; foreach my $player (@vob_not) { printf( "%-20s\n", $player->field( 'name')); } foreach my $player (@vob_none) { printf( "%-20s....none\n", $player->field( 'name')); } print "\n", join( ', ', sort keys %letters), "\n", "-" x 20, "\n"; foreach my $player (@vob_letter) { printf( "%-20s....%1s\n", $player->field( 'name'), $player->att( ' +vob')); } __DATA__ <stats><player vob="d" ><name>Houston, Allan</name><g>69</g></player> <player vob="z" ><name>Sprewell, Latrell</name><g>69</g></player> <player vob="a" ><name>Ewing, Patrick</name><g>49</g></player> <player vob="none" ><name >Johnson, Larry</name><g>57</g></player> <player vob="g" ><name>Camby, Marcus</name><g>48</g></player> <player vob="d" ><name>Thomas, Kurt</name><g>67</g><ppg>7.9</ppg></pla +yer> <player vob="none" ><name >Ward, Charlie</name><g>59</g><ppg>7.5</ppg> +</player> <player vob="none" ><name>Wallace, John</name><g>55</g><ppg>6.6</ppg>< +/player> <player><name>Childs, Chris</name><g>61</g><ppg>5.3</ppg></player> <player><name>Duncan, Tim</name><g>66</g><ppg>23.1</ppg></player> <player><name>Robinson, David</name><g>68</g><ppg>17.1</ppg></player> </stats>

Update: note that this is not really the most efficient way, XML::Twig will go through the entire list of children for each list it builds), just the easiest to write.


In reply to Re: XML::Twig -- sorting by attribute by mirod
in thread XML::Twig -- sorting by attribute by Novitiatus

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.