title: XSLT sorting - how to sort AND copy the whole input as sorted output
body: <p>I am trying to sort an XML file using XSLT transformation, but it doesn't do what I want. I want to sort the XML lexicographically by the <code>loc</code> element.</p>
  
  <p>This is my original XML:
  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;
      &lt;url&gt;
          &lt;loc&gt;url3&lt;/loc&gt;
      &lt;/url&gt;
      &lt;url&gt;
          &lt;loc&gt;url2&lt;/loc&gt;
      &lt;/url&gt;
      &lt;url&gt;
          &lt;loc&gt;url1&lt;/loc&gt;
      &lt;/url&gt;
  &lt;/urlset&gt;
  </code></pre>
  
  <p>This is the xslt:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
      &lt;xsl:output method="xml" encoding="utf-8" indent="yes"
          version="1.0" /&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="urlset"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*" /&gt;
              &lt;xsl:apply-templates select="url"&gt;
                  &lt;xsl:sort select="loc" data-type="text" order="ascending" /&gt;
              &lt;/xsl:apply-templates&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>And I'd like to see this output:
  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;
      &lt;url&gt;
          &lt;loc&gt;url1&lt;/loc&gt;
      &lt;/url&gt;
      &lt;url&gt;
          &lt;loc&gt;url2&lt;/loc&gt;
      &lt;/url&gt;
      &lt;url&gt;
          &lt;loc&gt;url3&lt;/loc&gt;
      &lt;/url&gt;
  &lt;/urlset&gt;
  </code></pre>
  
  <p>I tried to follow this question, but I can't get it working.</p>
  
  <p><a href="https://stackoverflow.com/questions/572854/how-to-sort-a-subelement-of-xml-with-xslt">How to sort a subelement</a></p>
  
  <p>The XSLT above just copies the original xml without any sorting and that's it. Can somebody help me? How can I sort the <code>loc</code> tags?</p>