title: XSLT Sorting - how to sort xml childnodes inside a parent node with an attribute
body: <p>What started out as a simple thing has turned out quite troublesome for XSLT noob. </p>
  
  <p>Trying to sort childnodes/descending but, after adding an attribute to their parent node, I receive an error when debugging in VS2010:</p>
  
  <p><code>"Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added."</code></p>
  
  <p>Suppose I have this simple XML:</p>
  
  <pre><code>&lt;posts&gt;
      &lt;year value="2013"&gt;
          &lt;post postid="10030" postmonth="1"&gt;
              &lt;othernode&gt;&lt;/othernode&gt;
              &lt;othernode2&gt;&lt;/othernode2&gt;
          &lt;/post&gt;
          &lt;post postid="10040" postmonth="2"&gt;
              &lt;othernode&gt;&lt;/othernode&gt;
              &lt;othernode2&gt;&lt;/othernode2&gt;
          &lt;/post&gt;
          &lt;post postid="10050" postmonth="3"&gt;
               &lt;othernode&gt;&lt;/othernode&gt;
               &lt;othernode2&gt;&lt;/othernode2&gt;
          &lt;/post&gt;
      &lt;/year&gt;
      &lt;year value="2012"&gt;
          &lt;post postid="10010" postmonth="1"&gt;
              &lt;othernode&gt;&lt;/othernode&gt;
              &lt;othernode2&gt;&lt;/othernode2&gt;
          &lt;/post&gt;
          &lt;post postid="10015" postmonth="2"&gt;
              &lt;othernode&gt;&lt;/othernode&gt;
              &lt;othernode2&gt;&lt;/othernode2&gt;
          &lt;/post&gt;
          &lt;post postid="10020" postmonth="3"&gt;
               &lt;othernode&gt;&lt;/othernode&gt;
               &lt;othernode2&gt;&lt;/othernode2&gt;
          &lt;/post&gt;
      &lt;/year&gt;
  &lt;/posts&gt;
  </code></pre>
  
  <p>I pass a XPATH to a xmldatasource to retrieve the relevant <code>&lt;year&gt;</code> node, e.g. 2013. 
  Then I need to sort its child <code>&lt;post&gt;</code> nodes descending using postid, so for <code>&lt;year value=2013&gt;</code>, postid=10050 would show up first when rendered. </p>
  
  <p>So, to be clear: I'm only interested in sorting inside one <code>&lt;year&gt;</code> node. </p>
  
  <p>Before I split the  nodes into separate  nodes (i.e. xml was /posts/post) the following XSLT worked:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
      &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="no"/&gt;
      &lt;xsl:strip-space elements="*"/&gt;
      &lt;xsl:template match="@*|node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*|node()"&gt;
                  &lt;xsl:sort select="@postid" data-type="text" order="descending"/&gt;
              &lt;/xsl:apply-templates&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Now the xmldatasource is empty when running due to the above error. If I pass ascending into the order the same xml is returned obviously (no transformation)</p>
  
  <p>Question: how to update the XSLT above (or new) to accommodate the parent node attribute (<code>&lt;year value=""&gt;</code>)? Through research, an answer said "I need to added attribute creation before element creation". This makes sense as watching the debugger, the childnodes are formed in desc order, but the year tag is missing its attribute. But I don't have a clue really about XSLT. Can't see it being too complicated but just don't know the language. </p>
  
  <p>Any help, greatly appreciated. Thanks</p>