title: Sort XML nodes in alphabetical order using XSL
body: <p>I am trying to figure out  how to sort the XML list of employees alphabetically by lastname using XSL. Right now it just displays the XML information in the same order as it is in the XML. I don't think I fully understand how to use the <code>&lt;xsl:sort&gt;</code>  function as I am new to XSL. I also tried putting  <code>order-by="+ Lastname"</code> in with <code>&lt;xsl:for-each&gt;</code>  and I couldn't get that to work either. </p>
  
  <p>Heres my xml: </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;?xml-stylesheet type="text/xsl" href="Company1.xsl"?&gt;
  &lt;Company&gt;
      &lt;Employee&gt;
          &lt;Firstname&gt;John&lt;/Firstname&gt;
          &lt;Lastname&gt;Smith&lt;/Lastname&gt;
          &lt;ssn&gt;635-35-7463&lt;/ssn&gt;
          &lt;doh&gt;February 3, 2011&lt;/doh&gt;
          &lt;Age&gt;34&lt;/Age&gt;
      &lt;/Employee&gt;
      &lt;Employee&gt;
          &lt;Firstname&gt;Brad&lt;/Firstname&gt;
          &lt;Lastname&gt;Roberts&lt;/Lastname&gt;
          &lt;ssn&gt;789-65-4568&lt;/ssn&gt;
          &lt;doh&gt;February 13, 2012&lt;/doh&gt;
          &lt;Age&gt;25&lt;/Age&gt;
      &lt;/Employee&gt;
      &lt;Employee&gt;
          &lt;Firstname&gt;Karen&lt;/Firstname&gt;
          &lt;Lastname&gt;Smith&lt;/Lastname&gt;
          &lt;ssn&gt;369-12-7415&lt;/ssn&gt;
          &lt;doh&gt;March 24, 2011&lt;/doh&gt;
          &lt;Age&gt;28&lt;/Age&gt;
      &lt;/Employee&gt;
      &lt;Employee&gt;
          &lt;Firstname&gt;Eli&lt;/Firstname&gt;
          &lt;Lastname&gt;Smith&lt;/Lastname&gt;
          &lt;ssn&gt;489-32-8525&lt;/ssn&gt;
          &lt;doh&gt;September 14, 2010&lt;/doh&gt;
          &lt;Age&gt;38&lt;/Age&gt;
      &lt;/Employee&gt;
      &lt;Employee&gt;
          &lt;Firstname&gt;Bill&lt;/Firstname&gt;
          &lt;Lastname&gt;Joel&lt;/Lastname&gt;
          &lt;ssn&gt;689-67-7634&lt;/ssn&gt;
          &lt;doh&gt;February 29, 2012&lt;/doh&gt;
          &lt;Age&gt;24&lt;/Age&gt;
      &lt;/Employee&gt;
      &lt;Employee&gt;
          &lt;Firstname&gt;Kelly&lt;/Firstname&gt;
          &lt;Lastname&gt;Greene&lt;/Lastname&gt;
          &lt;ssn&gt;927-82-6873&lt;/ssn&gt;
          &lt;doh&gt;December 3, 2010&lt;/doh&gt;
          &lt;Age&gt;34&lt;/Age&gt;
      &lt;/Employee&gt;
  &lt;/Company&gt;
  </code></pre>
  
  <p>And heres my XSL:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt; 
  &lt;xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
      &lt;xsl:template match="/"&gt;
      &lt;HTML&gt;
      &lt;HEAD&gt;
      &lt;TITLE&gt;Company Employees&lt;/TITLE&gt; 
      &lt;/HEAD&gt;
      &lt;BODY&gt;
      &lt;H2&gt;Company Employees&lt;/H2&gt; 
  
      &lt;xsl:for-each select="Company/Employee"&gt;
          &lt;xsl:sort select="Employee/Lastname" data-type="text" order="ascending"/&gt;
          &lt;xsl:sort select="Employee/Firstname" data-type="text" order="ascending"/&gt;
  
          &lt;SPAN STYLE="font-weight:bold"&gt;FirstName: &lt;/SPAN&gt;
              &lt;xsl:value-of select="Lastname" /&gt;
          &lt;BR /&gt;
          &lt;SPAN STYLE="font-weight:bold"&gt;LastName: &lt;/SPAN&gt;
              &lt;xsl:value-of select="Firstname" /&gt;
          &lt;BR /&gt; 
          &lt;SPAN STYLE="font-weight:bold"&gt;SSN: &lt;/SPAN&gt; 
          &lt;xsl:value-of select="ssn" /&gt; 
          &lt;BR /&gt; 
          &lt;SPAN STYLE="font-weight:bold"&gt;Date of Hire: &lt;/SPAN&gt; 
          &lt;xsl:value-of select="doh" /&gt; 
          &lt;BR /&gt; 
          &lt;SPAN STYLE="font-weight:bold"&gt;Age: &lt;/SPAN&gt; 
          &lt;xsl:value-of select="Age" /&gt; 
          &lt;P/&gt;
      &lt;/xsl:for-each&gt;
      &lt;/BODY&gt;
      &lt;/HTML&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>