title: XSLT 1.0: apply-templates and template mode
body: <p>I have the following XML:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;Order&gt;
  &lt;Item&gt;
      &lt;RECORD_ID&gt;RECORD_ID&lt;/RECORD_ID&gt;
      &lt;ENTITY_CODE&gt;ENTITY_CODE&lt;/ENTITY_CODE&gt;
      &lt;USER_CODE&gt;USER_CODE&lt;/USER_CODE&gt;
      &lt;RECORD_DATE&gt;RECORD_DATE&lt;/RECORD_DATE&gt;
      &lt;ITEM_CODE&gt;ITEM_CODE&lt;/ITEM_CODE&gt;
      &lt;LINE_QUANTITY&gt;LINE_QUANTITY&lt;/LINE_QUANTITY&gt;
      &lt;LINE_FREE_STOCK&gt;LINE_FREE STOCK&lt;/LINE_FREE_STOCK&gt;
      &lt;LINE_PRICE&gt;LINE_PRICE&lt;/LINE_PRICE&gt;
      &lt;LINE_DISCOUNT_PERCENT&gt;LINE_DISCOUNT PERCENT&lt;/LINE_DISCOUNT_PERCENT&gt;
  &lt;/Item&gt;
  &lt;Item&gt;
      &lt;RECORD_ID&gt;9046&lt;/RECORD_ID&gt;
      &lt;ENTITY_CODE&gt;12010601&lt;/ENTITY_CODE&gt;
      &lt;USER_CODE&gt;122&lt;/USER_CODE&gt;
      &lt;RECORD_DATE&gt;2011-08-24&lt;/RECORD_DATE&gt;
      &lt;ITEM_CODE&gt;804-008165&lt;/ITEM_CODE&gt;
      &lt;LINE_QUANTITY&gt;2&lt;/LINE_QUANTITY&gt;
      &lt;LINE_FREE_STOCK&gt;1&lt;/LINE_FREE_STOCK&gt;
  &lt;/Item&gt;
  &lt;Item&gt;
      &lt;RECORD_ID&gt;9046&lt;/RECORD_ID&gt;
      &lt;ENTITY_CODE&gt;12010601&lt;/ENTITY_CODE&gt;
      &lt;USER_CODE&gt;122&lt;/USER_CODE&gt;
      &lt;RECORD_DATE&gt;2011-08-24&lt;/RECORD_DATE&gt;
      &lt;ITEM_CODE&gt;804-008161&lt;/ITEM_CODE&gt;
      &lt;LINE_QUANTITY&gt;1&lt;/LINE_QUANTITY&gt;
      &lt;LINE_FREE_STOCK&gt;1&lt;/LINE_FREE_STOCK&gt;
  &lt;/Item&gt;
  &lt;Item&gt;
      &lt;RECORD_ID&gt;9046&lt;/RECORD_ID&gt;
      &lt;ENTITY_CODE&gt;12010601&lt;/ENTITY_CODE&gt;
      &lt;USER_CODE&gt;122&lt;/USER_CODE&gt;
      &lt;RECORD_DATE&gt;2011-08-24&lt;/RECORD_DATE&gt;
      &lt;ITEM_CODE&gt;804-008225&lt;/ITEM_CODE&gt;
      &lt;LINE_QUANTITY&gt;5&lt;/LINE_QUANTITY&gt;
  &lt;/Item&gt;
  &lt;/Order&gt;
  </code></pre>
  <p>Sometimes within the <code>item</code> tag I have the element <code>&lt;LINE_FREE_STOCK&gt;</code>. If that occurs I have to create an additional position in the output XML.</p>
  <p>Now I came up with this style sheet:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;&gt;
  &lt;xsl:output encoding=&quot;UTF-8&quot; method=&quot;xml&quot; indent=&quot;yes&quot;/&gt;
  
  &lt;xsl:template match=&quot;/&quot;&gt;
      &lt;ORDERS05&gt;
          &lt;IDOC BEGIN=&quot;1&quot;&gt;
              &lt;xsl:apply-templates select=&quot;Order&quot;/&gt;
          &lt;/IDOC&gt;
      &lt;/ORDERS05&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;Order&quot;&gt;
      &lt;Header&gt;
          &lt;xsl:value-of select=&quot;'some header data'&quot;/&gt;
      &lt;/Header&gt;
      &lt;xsl:apply-templates select=&quot;Item[position() &gt;1]&quot;/&gt;
      &lt;xsl:apply-templates select=&quot;Item[position() &gt;1 and child::LINE_FREE_STOCK]&quot; mode=&quot;freestock&quot;/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;Item&quot;&gt;
          &lt;position&gt;
              &lt;item&gt;
                  &lt;number&gt;&lt;xsl:value-of select=&quot;ITEM_CODE&quot;/&gt;&lt;/number&gt;
                  &lt;quantity&gt;&lt;xsl:value-of select=&quot;LINE_QUANTITY&quot;/&gt;&lt;/quantity&gt;
              &lt;/item&gt;
          &lt;/position&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;Item[position() &gt;1 and child::LINE_FREE_STOCK]&quot; mode=&quot;freestock&quot;&gt;
          &lt;position&gt;
              &lt;item&gt;
                  &lt;number&gt;&lt;xsl:value-of select=&quot;ITEM_CODE&quot;/&gt;&lt;/number&gt;
                  &lt;freestock_quant&gt;&lt;xsl:value-of select=&quot;LINE_FREE_STOCK&quot;/&gt;&lt;/freestock_quant&gt;
              &lt;/item&gt;
          &lt;/position&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>It creates this (simplified) wanted output:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;ORDERS05&gt;
  &lt;IDOC BEGIN=&quot;1&quot;&gt;
      &lt;Header&gt;some header data&lt;/Header&gt;
      &lt;position&gt;
          &lt;item&gt;
              &lt;number&gt;804-008165&lt;/number&gt;
              &lt;quantity&gt;2&lt;/quantity&gt;
          &lt;/item&gt;
      &lt;/position&gt;
      &lt;position&gt;
          &lt;item&gt;
              &lt;number&gt;804-008161&lt;/number&gt;
              &lt;quantity&gt;1&lt;/quantity&gt;
          &lt;/item&gt;
      &lt;/position&gt;
      &lt;position&gt;
          &lt;item&gt;
              &lt;number&gt;804-008225&lt;/number&gt;
              &lt;quantity&gt;5&lt;/quantity&gt;
          &lt;/item&gt;
      &lt;/position&gt;
      &lt;position&gt;
          &lt;item&gt;
              &lt;number&gt;804-008165&lt;/number&gt;
              &lt;freestock_quant&gt;1&lt;/freestock_quant&gt;
          &lt;/item&gt;
      &lt;/position&gt;
      &lt;position&gt;
          &lt;item&gt;
              &lt;number&gt;804-008161&lt;/number&gt;
              &lt;freestock_quant&gt;1&lt;/freestock_quant&gt;
          &lt;/item&gt;
      &lt;/position&gt;
  &lt;/IDOC&gt;
  &lt;/ORDERS05&gt;
  </code></pre>
  <p>804-008165 and 804-008161 show up twice - once as a standard item and once as the free stock item with the respective quantities.</p>
  <p>But did I forget anything here? Is there some sort of pitfall I don't see?
  Is that XSLT robust enough?</p>