title: Sort XML to XML using XSLT
body: <p>I have a found a few similar questions to this,
  but struggled to 'bend' the solution to what I need,
  so apologies for asking again.</p>
  <p>I have some XML like this:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;ns:Root
      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xmlns:ns=&quot;urn:Test.Namespace&quot;  
      xsi:schemaLocation=&quot;urn:Test.Namespace Test1.xsd&quot;
      &gt;
      &lt;ns:element1 id=&quot;001&quot;&gt;
          &lt;ns:element2 id=&quot;001.1&quot; order=&quot;1&quot;&gt;
              &lt;ns:element3 id=&quot;001.1.1&quot; /&gt;
          &lt;/ns:element2&gt;
          &lt;ns:element2 id=&quot;001.2&quot; order=&quot;2&quot;&gt;
              &lt;ns:element3 id=&quot;001.1.2&quot; /&gt;
          &lt;/ns:element2&gt;        
      &lt;/ns:element1&gt;
      &lt;ns:element1 id=&quot;003&quot;&gt;
          &lt;ns:element2 id=&quot;007.0&quot; order=&quot;1&quot;&gt;
              &lt;ns:element3 id=&quot;007.1.1&quot; /&gt;
          &lt;/ns:element2&gt;
      &lt;/ns:element1&gt;
      &lt;ns:element1 id=&quot;002&quot;&gt;
          &lt;ns:element2 id=&quot;002.1&quot; order=&quot;3&quot;&gt;
              &lt;ns:element3 id=&quot;002.1.1&quot; /&gt;
          &lt;/ns:element2&gt;
          &lt;ns:element2 id=&quot;002.2&quot; order=&quot;4&quot;&gt;
              &lt;ns:element3 id=&quot;002.1.2&quot; /&gt;
          &lt;/ns:element2&gt; 
      &lt;/ns:element1&gt;    
  &lt;/ns:Root&gt;
  </code></pre>
  <p>I have written this XSLT:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;xsl:stylesheet version=&quot;1.0&quot;
                  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
                  xmlns:ns=&quot;urn:Test.Namespace&quot;
                  &gt;
      &lt;xsl:output indent=&quot;no&quot; /&gt;
      &lt;xsl:template match=&quot;text()[not(string-length(normalize-space()))]&quot;/&gt;
      &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
  
      &lt;xsl:template match=&quot;/&quot;&gt;
          &lt;xsl:apply-templates&gt;
              &lt;xsl:sort select=&quot;/ns:Root/ns:element1/@id&quot; /&gt;
              &lt;xsl:copy-of select=&quot;.&quot; /&gt;
          &lt;/xsl:apply-templates&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match=&quot;ns:element1&quot;&gt;
          &lt;xsl:copy-of select=&quot;.&quot; /&gt;
          &lt;xsl:apply-templates /&gt;        
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match=&quot;ns:element2&quot;&gt;
          &lt;xsl:copy-of select=&quot;.&quot; /&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match=&quot;ns:element3&quot;&gt;
          &lt;xsl:copy-of select=&quot;.&quot; /&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>(I cribbed the outline for this from here <a href="https://stackoverflow.com/questions/1908393/how-to-sort-xml">how to sort xml?</a>)</p>
  <p>What I want to be able to do, is use this XSLT to sort my original XML by the <code>id</code> attribute of <code>element1</code> and produce XML.
  The idea being that once it is sorted, I can process it with some other XSLT to get the final result.</p>
  <p>Unfortunately, this does not give me any output, which makes me think there is a really stupid typo somewhere, but I cannot see it.</p>