I would like to sort based on an attribute. I am working on an automatic
derived object publishing system using ClearCase for sharing do's across
different types of automated build systems of which some may not be using
ClearCase. Since my perl program is over a 1000 lines, I thought an
existing example from the xmltwig site would help.
I added an attribute called vob. Note: not all names have this attribute
because it also has to publish to a simple directory structure.
<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></player>
<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>
I would like them to be ordered like this:
No attribute or vob=none
--------------
Childs, Chris
Duncan, Tim
Robinson, David
Johnson, Larry ....none
Ward, Charlie ....none
Wallace, John ....none
a,d,g,none,z
--------------
Ewing, Patrick ....a
Houston, Allan ....d
Thomas, Kurt ....d
Camby, Marcus ....g
Sprewell, Latrell ....z
Here is the example for sorting.
In the example below, the sort command is numerically sorting on first child.
My current plan is as follows:
Run through the entire players array collecting the values for the vob attribute,
and then sort and make unique my attribute values. While looping through the
unique vob attribute values, loop through the players array testing
to see if each player matched the attribute.
I was hoping for something more elegant.
Any suggestions would be appreciated.
Thanks,
Patrick
#########################################################################
# #
# This first example shows how to create a twig, parse a file into it #
# get the root of the document, its children, access a specific child #
# and get the text of an element #
# #
#########################################################################
use strict;
use XML::Twig;
my $field= $ARGV[0] || 'name';
my $twig= new XML::Twig;
$twig->parsefile( "nba.xml"); # build the twig
my $root= $twig->root; # get the root of the twig (stats)
my @players= $root->children; # get the player list
# sort it on the text of the field
my @sorted= sort { $b->first_child( $field)->text
<=> $a->first_child( $field)->text }
@players;
print '<?xml version="1.0"?>'; # print the XML declaration
print '<!DOCTYPE stats SYSTEM "stats.dtd" []>';
print '<stats>'; # then the root element start tag
foreach my $player (@sorted) # the sorted list
{ $player->print; # print the xml content of the element
print "\n";
}
print "</stats>\n"; # close the document
In reply to XML::Twig -- sorting by attribute by Novitiatus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |