title: Order of nodes in XSL Transformation
body: <p>I have a xml which looks like below :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Object&gt;
  &lt;Data type="plan" name="testAom" id="10"&gt;
  
      &lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-1" operation="create" &gt;
          &lt;p name="Active"&gt;1&lt;/p&gt;
          &lt;p name="Type"&gt;CPU&lt;/p&gt;
          &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
          &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
      &lt;/obj&gt;
  
      &lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-1/E-1" operation="create"&gt;
          &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
          &lt;p name="interval"&gt;10&lt;/p&gt;
      &lt;/obj&gt;
  
      &lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-2/E-1" operation="update"&gt;
          &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
          &lt;p name="interval"&gt;10&lt;/p&gt;
      &lt;/obj&gt;
  
      &lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-2" operation="update"&gt;
          &lt;p name="Active"&gt;1&lt;/p&gt;
          &lt;p name="Type"&gt;CPU&lt;/p&gt;
          &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
          &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
      &lt;/obj&gt;
  &lt;/Data&gt;
  &lt;/Object&gt;
  </code></pre>
  
  <p>Here the order of the input source file is not guaranteed. But in the output after an XSL transformation, i require the output to be in certain order.</p>
  
  <p>Below is the XSL:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:output method="xml" 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;xsl:template match="obj[@class = 'D' ]"&gt;
  &lt;xsl:variable name="item" select="."/&gt;
  &lt;xsl:choose&gt;
      &lt;xsl:when test="$item/@operation='update'"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*"/&gt;
              &lt;xsl:attribute name="operation"&gt;delete&lt;/xsl:attribute&gt;
              &lt;xsl:apply-templates select="node()"/&gt;
          &lt;/xsl:copy&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*"/&gt;
              &lt;xsl:attribute name="operation"&gt;create&lt;/xsl:attribute&gt;
              &lt;xsl:apply-templates select="node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*"/&gt;
              &lt;xsl:attribute name="operation"&gt;NEW_create&lt;/xsl:attribute&gt;
              &lt;xsl:apply-templates select="node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="obj[@class = 'E' ]"&gt;
  &lt;xsl:variable name="childitem" select="."/&gt;
  &lt;xsl:choose&gt;
      &lt;xsl:when test="$childitem/@operation='update'"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*"/&gt;
              &lt;xsl:attribute name="operation"&gt;delete&lt;/xsl:attribute&gt;
              &lt;xsl:apply-templates select="node()"/&gt;
          &lt;/xsl:copy&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*"/&gt;
              &lt;xsl:attribute name="operation"&gt;create&lt;/xsl:attribute&gt;
              &lt;xsl:apply-templates select="node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*"/&gt;
              &lt;xsl:attribute name="operation"&gt;NEW_create&lt;/xsl:attribute&gt;
              &lt;xsl:apply-templates select="node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;         
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The output i get is :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Object&gt;
  &lt;Data type="plan" name="testAom" id="10"&gt;
  
      &lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-1" operation="NEW_create"&gt;
          &lt;p name="Active"&gt;1&lt;/p&gt;
          &lt;p name="Type"&gt;CPU&lt;/p&gt;
          &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
          &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
      &lt;/obj&gt;
  
      &lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-1/E-1" operation="NEW_create"&gt;
          &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
          &lt;p name="interval"&gt;10&lt;/p&gt;
      &lt;/obj&gt;
  
      &lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-2/E-1" operation="delete"&gt;
          &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
          &lt;p name="interval"&gt;10&lt;/p&gt;
      &lt;/obj&gt;
      &lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-2/E-1" operation="create"&gt;
          &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
          &lt;p name="interval"&gt;10&lt;/p&gt;
      &lt;/obj&gt;
  
      &lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-2" operation="delete"&gt;
          &lt;p name="Active"&gt;1&lt;/p&gt;
          &lt;p name="Type"&gt;CPU&lt;/p&gt;
          &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
          &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
      &lt;/obj&gt;
      &lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-2" operation="create"&gt;
          &lt;p name="Active"&gt;1&lt;/p&gt;
          &lt;p name="Type"&gt;CPU&lt;/p&gt;
          &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
          &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
      &lt;/obj&gt;
  &lt;/Data&gt;
  &lt;/Object&gt;
  </code></pre>
  
  <p>Here when the <strong>operation</strong> attribute is <strong>"update"</strong>, i need to do a delete and create.</p>
  
  <p>The nodes above  have a parent child relationship based on attribute <strong>distName</strong>, ie <strong>A-1/B-1/C-1/D-1</strong> is parent of <strong>A-1/B-1/C-1/D-1/E-1</strong>.
  For the update operation to work correct, i need the parent create and delete first and then followed by child delete and create always.</p>
  
  <p><strong>How can this be achieved irrespective of the order of input source xml file??</strong></p>
  
  <p>ie. obj nodes with class attribute <strong>D</strong> should be first in order than <strong>E</strong>.</p>
  
  <p>Expected Output XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Object&gt;
  &lt;Data type="plan" name="testAom" id="10"&gt;
  &lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-1" operation="NEW_create"&gt;
  &lt;p name="Active"&gt;1&lt;/p&gt;
  &lt;p name="Type"&gt;CPU&lt;/p&gt;
  &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
  &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
  &lt;/obj&gt;
  &lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-1/E-1" operation="NEW_create"&gt;
  &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
  &lt;p name="interval"&gt;10&lt;/p&gt;
  &lt;/obj&gt;
  &lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-2" operation="delete"&gt;
  &lt;p name="Active"&gt;1&lt;/p&gt;
  &lt;p name="Type"&gt;CPU&lt;/p&gt;
  &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
  &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
  &lt;/obj&gt;&lt;obj class="D" version="1.0" distName="A-1/B-1/C-1/D-2" operation="create"&gt;
  &lt;p name="Active"&gt;1&lt;/p&gt;
  &lt;p name="Type"&gt;CPU&lt;/p&gt;
  &lt;p name="StDate"&gt;2013-07-27T00:00:00+00:00&lt;/p&gt;
  &lt;p name="StpDate"&gt;2013-07-29T00:00:00+00:00&lt;/p&gt;
  &lt;/obj&gt;
  &lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-2/E-1" operation="delete"&gt;
  &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
  &lt;p name="interval"&gt;10&lt;/p&gt;
  &lt;/obj&gt;&lt;obj class="E" version="1.0" distName="A-1/B-1/C-1/D-2/E-1" operation="create"&gt;
  &lt;p name="dayOfWeek"&gt;0&lt;/p&gt;
  &lt;p name="interval"&gt;10&lt;/p&gt;
  &lt;/obj&gt;
  &lt;/Data&gt;
  &lt;/Object&gt;
  </code></pre>