title: How to apply multiple template definitions where each of them is changing the same XML structure
body: <p>In this code example I have 2 challenges one is to set B1 node after B node and before node C, D and E and the second challenge is to Add the second KEY node to the /ROOT/E/OTHER/DEAL/KEYS structure.</p>
  
  <p>This XML sample:</p>
  
  <pre><code>        &lt;ROOT&gt;
            &lt;A&gt;some A text&lt;/A&gt;
            &lt;B&gt;some B text&lt;/B&gt;
            &lt;C&gt;some C text&lt;/C&gt;
            &lt;D&gt;some D text&lt;/D&gt;
            &lt;E&gt;
              &lt;OTHER&gt;
                &lt;DEAL&gt;
                  &lt;KEYS&gt;
                    &lt;KEY&gt;
                      &lt;KeyIdentifierType&gt;KeyIdentifierTypeA&lt;/KeyIdentifierType&gt;
                      &lt;KeyValue&gt;123456|1&lt;/KeyValue&gt;
                    &lt;/KEY&gt;
                  &lt;/KEYS&gt;
                &lt;/DEAL&gt;
              &lt;/OTHER&gt;
            &lt;/E&gt;
          &lt;/ROOT&gt;
  </code></pre>
  
  <p>after the transformation:</p>
  
  <pre><code>    &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
          &lt;xsl:strip-space elements="*"/&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;!-- Identifiers are added by the system. Need to pass parms from the calling program --&gt;
          &lt;xsl:template match="ROOT" name="add-B1"&gt;
              &lt;xsl:variable name="elements-after" select="C|D|E"/&gt;
              &lt;xsl:copy&gt;
                  &lt;xsl:copy-of select="* except $elements-after"/&gt;
                  &lt;B1&gt;some B1 text&lt;/B1&gt;
                  &lt;xsl:copy-of select="$elements-after"/&gt;
              &lt;/xsl:copy&gt;
          &lt;/xsl:template&gt;
  
          &lt;!-- KEY is added by the system. Need to pass parms from the calling program --&gt;
          &lt;xsl:template match="ROOT/E/OTHER/DEAL/KEYS" name="add-KEYS"&gt;
              &lt;xsl:param name="KeyIdentifierTypeB"&gt;654321|1&lt;/xsl:param&gt;
              &lt;xsl:copy&gt;
                  &lt;xsl:copy-of select="*"/&gt;
                  &lt;KEY&gt;
                      &lt;KeyIdentifierType&gt;KeyIdentifierTypeB&lt;/KeyIdentifierType&gt;
                      &lt;KeyValue&gt;
                          &lt;xsl:value-of select="$KeyIdentifierTypeB"/&gt;
                      &lt;/KeyValue&gt;
                  &lt;/KEY&gt;
              &lt;/xsl:copy&gt;
          &lt;/xsl:template&gt;
      &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>become:</p>
  
  <pre><code>        &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;ROOT&gt;
              &lt;A&gt;some A text&lt;/A&gt;
              &lt;B&gt;some B text&lt;/B&gt;
              &lt;B1 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;some B1 text&lt;/B1&gt;
              &lt;C&gt;some C text&lt;/C&gt;
              &lt;D&gt;some D text&lt;/D&gt;
              &lt;E&gt;
                  &lt;OTHER&gt;
                      &lt;DEAL&gt;
                          &lt;KEYS&gt;
                              &lt;KEY&gt;
                                  &lt;KeyIdentifierType&gt;KeyIdentifierTypeA&lt;/KeyIdentifierType&gt;
                                  &lt;KeyValue&gt;123456|1&lt;/KeyValue&gt;
                              &lt;/KEY&gt;
                          &lt;/KEYS&gt;
                      &lt;/DEAL&gt;
                  &lt;/OTHER&gt;
              &lt;/E&gt;
          &lt;/ROOT&gt;
  </code></pre>
  
  <p>why the second template definition has been completely ignored?</p>
  
  <p>The first code college has been resolved B1 node is set after B node and before node C, D and E or in the other words B1 node is set and nodes that must be places after it are: C, D and E.<br>
  The second template match="ROOT/E/OTHER/DEAL/KEYS" that should satisfy the second challenge part: to Add the second KEY node to the /ROOT/E/OTHER/DEAL/KEYS structure, has been ignored completely.
  In addition to this fact, if you comment the first template match over the ROOT node, the second template match="ROOT/E/OTHER/DEAL/KEYS" will work correctly, it will actually add the additional key, but I don't know why the first template match always overrides the second one. I try the xsl:template match="ROOT/E/OTHER/DEAL/KEYS... and the xsl:for-each select=... and xsl:call-template name="add-KEYS" but nothing helped me out.</p>
  
  <p>I actually understand, that apply-templates, match nodes templates with higher structure with highest priority. Changing template place in the XSLT file will not have any impact, it will not read it in the exact line sequence, instead it will process it by matching priority.
  “apply-templates” for each matching template will made a change on the XML structure, implicitly it will made the "for-each" for us, but I don't how to "advice" the second template that the structure has been changed, and why I should do it, because the second template match is looking for a different XPath location, that btw has never been touched from the first template. Should I apply the template sequencing in my case?... and what is the best practice how to do it?</p>
  
  <p>Expected result:</p>
  
  <pre><code>        &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;ROOT&gt;
              &lt;A&gt;some A text&lt;/A&gt;
              &lt;B&gt;some B text&lt;/B&gt;
              &lt;B1&gt;some B1 text&lt;/B1&gt;
              &lt;C&gt;some C text&lt;/C&gt;
              &lt;D&gt;some D text&lt;/D&gt;
              &lt;E&gt;
                  &lt;OTHER&gt;
                      &lt;DEAL&gt;
                          &lt;KEYS&gt;
                              &lt;KEY&gt;
                                  &lt;KeyIdentifierType&gt;KeyIdentifierTypeA&lt;/KeyIdentifierType&gt;
                                  &lt;KeyValue&gt;123456|1&lt;/KeyValue&gt;
                              &lt;/KEY&gt;
                              &lt;KEY&gt;
                                  &lt;KeyIdentifierType&gt;KeyIdentifierTypeB&lt;/KeyIdentifierType&gt;
                                  &lt;KeyValue&gt;654321|1&lt;/KeyValue&gt;
                              &lt;/KEY&gt;
                          &lt;/KEYS&gt;
                      &lt;/DEAL&gt;
                  &lt;/OTHER&gt;
              &lt;/E&gt;
          &lt;/ROOT&gt;
  </code></pre>