title: Add child elements in particular order
body: <p>I am working on an xml to xml transform via XSLT. I Have the following:</p>
  
  <p><b>stylesheet.xsl</b></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns="http://www.test.org"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"  version="2.0"&gt; &lt;!-- xs namespace allows typed functions and parameters --&gt;
  &lt;xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8" omit-xml-declaration="yes"/&gt;
  &lt;xsl:strip-space elements="*" /&gt; 
  
  &lt;xsl:param name="other-id" select="Request/Order/OtherId" /&gt;
  
  &lt;xsl:template match="*"&gt;
      &lt;xsl:element name="{local-name()}"&gt;
          &lt;xsl:copy-of select="@*" copy-namespaces="no" /&gt;
          &lt;xsl:apply-templates select="@*|node()" /&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="@*|text()|comment()|processing-instruction()"&gt;
      &lt;xsl:copy /&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/*"&gt;
      &lt;xsl:apply-templates select="node()" /&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Details"&gt;
      &lt;xsl:element name="{local-name()}"&gt;
          &lt;xsl:element name="Signon&gt;
              &lt;xsl:element name="SignonDt&gt;2017-01-01&lt;/xsl:element&gt;
              &lt;xsl:element name="MessageQuantity"&gt;3&lt;/xsl:element&gt;
          &lt;xsl:element&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="NotificationRq"&gt;
      &lt;xsl:element name="{local-name()}"&gt;
          &lt;xsl:element name="RqUID"&gt;Test&lt;/xsl:element&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Policy/SDDCd" /&gt;
  
  &lt;xsl:template match="Policy"&gt;
      &lt;xsl:element name="{local-name()}"&gt;
          &lt;xsl:element name="RFDCd"&gt;
              &lt;xsl:call-template name="getRFDCd" /&gt;
          &lt;/xsl:element&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template name="getRFDCd"&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="contains($other-id, 'RFD 2')"&gt;
              &lt;xsl:text&gt;AUB&lt;/xsl:text&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:text&gt;CL&lt;/xsl:text&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Contact/Addr" &gt;
      &lt;xsl:element name="{local-name()}"&gt;
          &lt;xsl:element name="AddrTypeCd"&gt;StreetAddress&lt;/xsl:element&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;!-- other templates structured similar to these --&gt;
  </code></pre>
  
  <p><b>input.xml</b></p>
  
  <pre><code>&lt;Request&gt;
      &lt;Details&gt;
          &lt;NotificationRq&gt;
              &lt;RqDate&gt;2017-01-01&lt;/RqDate&gt;
              &lt;RqDetails&gt;
              &lt;!-- other children --&gt;
              &lt;RqDetails&gt;
          &lt;/NotificationRq&gt;
          &lt;Policy&gt;
              &lt;PolNumber&gt;1234567890&lt;/PolNumber&gt;
              &lt;SDDCd&gt;T35&lt;/SDDCd&gt;
          &lt;/Policy&gt;
          &lt;Contact&gt;
              &lt;Addr&gt;
                  &lt;AddrLn1&gt;Test address line 1&lt;/AddrLn1&gt;
                  &lt;AddrLn2&gt;Test address line 2&lt;/AddrLn2&gt;
                  &lt;PostCode&gt;AX12D3&lt;/PostCode&gt;
              &lt;/Addr&gt;
          &lt;/Contact&gt;
          &lt;!-- other children --&gt;
      &lt;/Details&gt;
  &lt;/Request&gt;
  </code></pre>
  
  <p><b>output.xml</b></p>
  
  <pre><code>&lt;Details xmlns="http://test.org"&gt;
      &lt;Signon&gt;
          &lt;SignonDt&gt;2017-01-01&lt;/Signon&gt;
          &lt;MessageQuantity&gt;3&lt;/MessageQuantity&gt;
      &lt;/Signon&gt;
      &lt;NotificationRq&gt;
          &lt;RqUId&gt;Test&lt;/RqUID&gt;
          &lt;RqDate&gt;2017-01-01&lt;/RqDate&gt;
          &lt;RqDetails&gt;
              &lt;!-- other children --&gt;
          &lt;RqDetails&gt;
      &lt;/NotificationRq&gt;
      &lt;Policy&gt;
          &lt;RFDCd&gt;CL&lt;/RFDCd&gt;
          &lt;PolNumber&gt;1234567890&lt;/PolNumber&gt;
      &lt;/Policy&gt;
      &lt;Contact&gt;
          &lt;Addr&gt;
              &lt;AddrTypeCd&gt;StreetAddress&lt;/AddrTypeCd&gt;
              &lt;AddrLn1&gt;Test address line 1&lt;/AddrLn1&gt;
              &lt;AddrLn2&gt;Test address line 2&lt;/AddrLn2&gt;
              &lt;PostCode&gt;AX12D3&lt;/PostCode&gt;
          &lt;/Addr&gt;
      &lt;/Contact&gt;
      &lt;!-- other children --&gt;
  &lt;/Details&gt;
  </code></pre>
  
  <p>Note: I am using <code>&lt;xsl:element name="{local-name()}"&gt;</code> due to an earlier issue; i am copying source xml which has no namespace to new output which does, and this solution, along with the extra template alongside the identity transform fixed the issue of a namespace attribute being added to all elements</p>
  
  <p>The problem is, this xml is sent downstream where schema validation occurs, and because of this, position of newly created child elements matter. As shown in the snippets above, new child elements are added as the first child of the parent. For some cases this is fine and this is where they should be, but for around half of the created elements, they must appear last(with the odd one or two requiring insertion at a specified position). Still using input.xml as an example, here is how the desired output should look:</p>
  
  <p><b>desired-output.xml</b></p>
  
  <pre><code>&lt;Details xmlns="http://test.org"&gt;
      &lt;NotificationRq&gt;
          &lt;RqDate&gt;2017-01-01&lt;/RqDate&gt;
          &lt;RqDetails&gt;
              &lt;!-- other children --&gt;
          &lt;RqDetails&gt;
          &lt;RqUId&gt;Test&lt;/RqUID&gt;
      &lt;/NotificationRq&gt;
      &lt;Signon&gt;
          &lt;SignonDt&gt;2017-01-01&lt;/Signon&gt;
          &lt;MessageQuantity&gt;3&lt;/MessageQuantity&gt;
      &lt;/Signon&gt;
      &lt;Policy&gt;
          &lt;PolNumber&gt;1234567890&lt;/PolNumber&gt;
          &lt;RFDCd&gt;CL&lt;/RFDCd&gt;
      &lt;/Policy&gt;
      &lt;Contact&gt;
          &lt;Addr&gt;
              &lt;AddrLn1&gt;Test address line 1&lt;/AddrLn1&gt;
              &lt;AddrLn2&gt;Test address line 2&lt;/AddrLn2&gt;
              &lt;AddrTypeCd&gt;StreetAddress&lt;/AddrTypeCd&gt;
              &lt;PostCode&gt;AX12D3&lt;/PostCode&gt;
          &lt;/Addr&gt;
      &lt;/Contact&gt;
      &lt;!-- other children --&gt;
  &lt;/Details&gt;
  </code></pre>
  
  <p>Is there a way to specify where in the existing child order of an element the new child should appear? Is one of my templates causing the insertion order to always be in the first position?</p>
  
  <p>Additional Info: I have seen a few questions on specific order insertion, but they usually seem to be for a sequence of elements, or a series of repeating elements, e.g. how do I insert another author element in a series of author elements, and the solutions tend to make use of a position function to determine if one has looped to the right index, and then insert. The xml I am working with is made up of unique elements which may hold a value or may contain several children(with some of those containing children etc). There are no repeating elements in the xml, so I don't think I can make use of a solution as described above(unless someone knows how to do such a thing for non repeating children of an element). Also, I am using Saxon HE version 9.7.0-8</p>