title: XSLT 1.0 sorting by child elements value
body: <p>I have tried around with this for far too long, but I am not getting my data sorted correctly. Basically I want to sort all my collectedKeys under the root element by their keyId. All other information should go unchanged into the output.</p>
  
  <p>Input:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
      &lt;base&gt;
          &lt;id&gt;a&lt;/id&gt;
          &lt;name&gt;b&lt;/name&gt;
          &lt;subId&gt;c&lt;/subId&gt;
      &lt;/base&gt;
      &lt;key&gt;lvl1&lt;/key&gt;
      &lt;key&gt;lvl3&lt;/key&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;100&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40001&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;200&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40002&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;100&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40003&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;300&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40004&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;additionalKeys&gt;50&lt;/additionalKeys&gt;
      &lt;additionalKeys&gt;70&lt;/additionalKeys&gt;
      &lt;ignoredKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;500&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;3&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;60001&lt;/keyStatus&gt;
              &lt;initLang&gt;true&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/ignoredKeys&gt;
      &lt;ignoredKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;600&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;1&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;50001&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/ignoredKeys&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
      &lt;base&gt;
          &lt;id&gt;a&lt;/id&gt;
          &lt;name&gt;b&lt;/name&gt;
          &lt;subId&gt;c&lt;/subId&gt;
      &lt;/base&gt;
      &lt;key&gt;lvl1&lt;/key&gt;
      &lt;key&gt;lvl3&lt;/key&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;100&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40001&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;100&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40003&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;200&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40002&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;collectedKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;300&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;4&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;40004&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/collectedKeys&gt;
      &lt;additionalKeys&gt;50&lt;/additionalKeys&gt;
      &lt;additionalKeys&gt;70&lt;/additionalKeys&gt;
      &lt;ignoredKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;500&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;3&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;60001&lt;/keyStatus&gt;
              &lt;initLang&gt;true&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/ignoredKeys&gt;
      &lt;ignoredKeys&gt;
          &lt;keyInformation&gt;
              &lt;keyId&gt;600&lt;/keyId&gt;
              &lt;content&gt;
                  &lt;contentID&gt;1&lt;/contentID&gt;
              &lt;/content&gt;
              &lt;keyStatus&gt;50001&lt;/keyStatus&gt;
              &lt;initLang&gt;false&lt;/initLang&gt;
          &lt;/keyInformation&gt;
      &lt;/ignoredKeys&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Unfortunately my current xslt is not doing the sorting and I cannot figure out why not.</p>
  
  <p>This is the XLST I am using:</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;!--                                                --&gt; 
    &lt;xsl:template match="collectedKeys"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select="@*"/&gt;
        &lt;xsl:apply-templates select="keyInformation"&gt;
          &lt;xsl:sort select="keyId" data-type="number"/&gt;
        &lt;/xsl:apply-templates&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Any help would be appreciated.</p>