title: XSLT sort input in arbitrary order
body: <p>I'm pretty new to XSLT. I did my fair amount of research on the web however I could not find enough information that suit my case.</p>
  
  <p>This is what I'm trying to achieve:
  I have a large XML document with 10 to 15 sections. The structure more or less looks like the following:</p>
  
  <pre><code>&lt;headerSection&gt;
      &lt;!-- lots of stuff in here --&gt;
  &lt;/headerSection&gt;
  &lt;containerSection&gt;
      &lt;bodySection&gt;
          &lt;container1&gt;
              &lt;container2&gt;
                  &lt;identifier code="12345"/&gt;
                  &lt;!-- lots of stuff in here --&gt;
              &lt;/container2&gt;
          &lt;/container1&gt;
          &lt;container1&gt;
              &lt;container2&gt;
                  &lt;identifier code="98765"/&gt;
                  &lt;!-- lots of stuff in here --&gt;
              &lt;/container2&gt;
          &lt;/container1&gt;
      &lt;/bodySection&gt;
  &lt;/containerSection&gt;
  </code></pre>
  
  <p>I have an XSL file that I apply to this in order to output a styled HTML page.
  Now, what I want to do is to render the children of the <code>&lt;bodySection&gt;</code> in an arbitrary order. The criteria for determining the order is the <code>code</code> attribute in the <code>&lt;identifier&gt;</code> element. <br>
  Let me clarify once more that the ordering should be arbitrary.</p>
  
  <p>I understand that I can do this with an <code>xsl:sort</code> directive, however I'm a huge XSL newbie and I don't quite get how to use it from the examples I've found online. <br> The main point is that I'm generating a styled HTML page, not "just" transforming the XML input. </p>
  
  <p>What looks like to be the closest to what I'm trying to do is this snippet:</p>
  
  <pre><code>&lt;xsl:param name="pOrder" select="'professionalsection,educationalsection'"/&gt;
  &lt;xsl:template match="node()|@*"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="node()|@*"&gt;
              &lt;xsl:sort select="string-length(
                                   substring-before(
                                      concat(',',$pOrder,','),
                                      concat(',',name(),',')))"/&gt;
          &lt;/xsl:apply-templates&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Unfortunately I'm not very sure about how to used it in my case.
  Intuitively, I would try and change the values pointed at in <code>select="'professionalsection,educationalsection'"</code> but is this the right approach?
  Any help is very appreciated!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>By looking at the example posted in the comment, I came up with this. I hope it gives a more clear picture of what I'm trying to do:</p>
  
  <pre><code>    &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/&gt;
  &lt;xsl:strip-space elements="*"/&gt;
  
  &lt;!-- identity transform --&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="bodySection"&gt;
      &lt;xsl:variable name="sort-order"&gt;|98765|12345|&lt;/xsl:variable&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@*|node()"&gt;
              &lt;xsl:sort select="string-length(substring-before($sort-order, concat('|', container1/container2/identifier/@code, '|')))" data-type="number" order="descending"/&gt;
          &lt;/xsl:apply-templates&gt;  
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The above snippet doesn't work yet. The result I'm trying to achieve would look like:</p>
  
  <pre><code>    &lt;headerSection&gt;
      &lt;!-- lots of stuff in here --&gt;
  &lt;/headerSection&gt;
  &lt;containerSection&gt;
      &lt;bodySection&gt;
          &lt;container1&gt;
              &lt;container2&gt;
                  &lt;identifier code="98765"/&gt;
                  &lt;!-- lots of stuff in here --&gt;
              &lt;/container2&gt;
          &lt;/container1&gt;
          &lt;container1&gt;
              &lt;container2&gt;
                  &lt;identifier code="12345"/&gt;
                  &lt;!-- lots of stuff in here --&gt;
              &lt;/container2&gt;
          &lt;/container1&gt;
      &lt;/bodySection&gt;
  &lt;/containerSection&gt;
  </code></pre>
  
  <p>...where the elements <code>container1</code> (along with their children) are swapped.</p>