title: Sorting XML by Attribute and Modifying
body: <p>First a little context: I use a collection management software, GCStar, to manage my digital library (comics/manga/films, you name it - it's pretty awesome except for books). Problem is, it doesn't let me sort the shelf by multiple keys, say by Series AND Episode number. Episodes added later will always show up lower in the shelf, grouped by Series.</p>
  
  <p>I pattered around the configurations and found that the .gcs file it uses is nothing but an XML (which I am only cursorily familiar with). Goes like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;collection type="GCTVepisodes" items="101" version="1.6.1"&gt;
   &lt;information&gt;
    &lt;maxId&gt;101&lt;/maxId&gt;
   &lt;/information&gt;
  
   &lt;item
    id="1"
    name="The Vice President Doesn't Say Anything about the Possibility of 
          Him Being the Main Character"
    series="Baccano"
    season="1"
    episode="1"
    ...
   &gt;
    &lt;synopsis&gt;It's 1931 and...&lt;/synopsis&gt;
   ...
   &lt;/item&gt;
   &lt;item ...
  </code></pre>
  
  <p>The program, far as I understand, will always order descending by ID (which increases whenever I add an episode). So I need a transform on this which will:</p>
  
  <ol>
  <li>Sort the XML by series, then season, then episode</li>
  <li>Change the id attributes accordingly, starting from 1 to end (also reset maxId based on that)</li>
  <li>Write it all out into identical format to another XML.</li>
  </ol>
  
  <p>How to do this (not talking about cut-pasting code here, obviously)? Can XSLT do all this stuff? Should I look into a tree-based parser in Perl? This is the weekend and I'm on a Linux machine, so open-source solutions running on UNIX would be nice - something in Perl would probably be best. What should I read up on?</p>
  
  <p>If I can't do this at home, well, I can always design a small datastage job at the office, but I'd seriously like a simpler solution.</p>
  
  <p>Thanks! :)</p>