title: Sorting with XSLT
body: <p><strong>UPDATE - New code at the bottom</strong></p>
  
  <p>I'm trying to figure out how to use the sort function to pull the most recent record from some XML data.  I'm very new to using XSLT and am running into a bunch of problems. Here's an example of my data...</p>
  
  <pre><code>&lt;content date="1/13/2011 1:21:00 PM"&gt;
      &lt;collection vo="promotion"&gt;
          &lt;data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/&gt;
          &lt;data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/&gt;
      &lt;/collection&gt;
  &lt;/content&gt;
  </code></pre>
  
  <p>What I want to do is sort the data by promotionid in decsending order and then ONLY ouput via HTML the promotionid that is greatest.  Here is along the lines of what I was trying</p>
  
  <p><strong>UPDATE - This is the latest version of the code that is still experiencing problems.</strong></p>
  
  <pre><code>&lt;html&gt;&lt;body&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="html"  encoding="UTF-8" /&gt;
      &lt;xsl:template match="content/collection/data"&gt;
          &lt;xsl:apply-templates&gt;
              &lt;xsl:sort select="promotionid" order="descending" data-type="number" /&gt;
          &lt;/xsl:apply-templates&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="content/collection"&gt;
          &lt;xsl:value-of select="data/@promotionid" /&gt;
      &lt;/xsl:template&gt; 
  &lt;/xsl:stylesheet&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>While this does return results what I am getting back is '64526' and NOT '64646'.</p>
  
  <p>Can anyone help?  Also I've seen examples online where you can sort by multiple fields.  It may be worth noting now, rather then asking later, that we may want to end up sorting by startdate rather than promotionid.  I have managed to come up with code to break out the date by YYYY, MM, and DD, but have no idea how I would even begin to use that aside from using those as my select paramater of the sort, but I don't know if that actually works or not.</p>
  
  <pre><code>Year
  &lt;xsl:value-of select="substring(substring-after(substring-after(data/@startdate,'/'),'/'),1,4)" /&gt;
  
  Month
  &lt;xsl:value-of select="substring-before(data/@startdate,'/')" /&gt;
  
  Day
  &lt;xsl:value-of select="substring-before(substring-after(data/@startdate,'/'),'/')" /&gt;
  </code></pre>
  
  <p>Thank in advance and I appologize my less than novice XSLT skills.</p>
  
  <p><strong>------------------------------------------------------</strong></p>
  
  <p>After some help here the code has changed, but is still not working as intended.  Here is the code...</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="html"  encoding="UTF-8" /&gt;
      &lt;xsl:template match="content/collection/"&gt;
              &lt;xsl:apply-templates&gt;
                  &lt;xsl:sort select="@promotionid" order="descending" data-type="number" /&gt;
              &lt;/xsl:apply-templates&gt;
          &lt;/xsl:template&gt;
  
   &lt;xsl:template match="content/collection/data"&gt;
          &lt;xsl:if test="position()=1"&gt;
                     &lt;xsl:value-of select="@promotionid"/&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>And I am still seeing the lesser value ouput rather than the greater.  Perhaps there is another way to do this with out sorting?  As I am open to that possibility as well.</p>
  
  <p><strong>1/14/11 10:37 Update</strong>
  *<em>-------------------------------------------------------------------</em>*
  Okay using this code now does indeed sort the data and output the highest promotionid number.  Thanks a Ton!</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
   &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt;
      &lt;xsl:template match="node()|@*"&gt;
        &lt;xsl:copy&gt;
           &lt;xsl:apply-templates select="node()|@*"/&gt;
        &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="collection"&gt;
        &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@*"/&gt;
          &lt;xsl:apply-templates select="data"&gt;
           &lt;xsl:sort select="@promotionid" data-type="number" order="descending"/&gt;
          &lt;/xsl:apply-templates&gt;
        &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  &lt;xsl:template match="content/collection/data"&gt;
          &lt;xsl:if test="position()=1"&gt;
                     &lt;xsl:value-of select="@promotionid"/&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Ignoring the promtionid now can you show me how I would sort, descending, by JUST the date?  I tried removing Unfortunately I know the dates should have a static length, but we have no control of the data we receive :-(</p>
  
  <p>Also can you recommend a book to start with to really can some better understanding of all this?  You've been a tremendous help!</p>