title: dynamic xml nodes using xslt
body: <p>I use a transformation in SAP in order to generate an XML file. I've already achieved that by referring only to explicit nodes. Now, the requirement would be to enhance the existing XML result by adding new nodes dynamically, based on a number of tags that can vary in number.
  Here is the XML source :</p>
  
  <pre><code>&lt;!-- edited with XMLSpy v2011 rel. 3 sp1 (http://www.altova.com)--&gt;
  &lt;asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"&gt;
      &lt;asx:values&gt;
          &lt;MSAPO01&gt;
              &lt;LOGICALID&gt;SAP-AMG&lt;/LOGICALID&gt;
              &lt;COMPONENT&gt;Whatever&lt;/COMPONENT&gt;
              &lt;CONFIRMATION&gt;Always&lt;/CONFIRMATION&gt;
              &lt;CREATIONDATE/&gt;
              &lt;BOID/&gt;
              &lt;SYNC_CONFIRM&gt;Never&lt;/SYNC_CONFIRM&gt;
              &lt;SYNC_ACTION&gt;Change&lt;/SYNC_ACTION&gt;
              &lt;DATAAREA&gt;
                  &lt;item&gt;
                      &lt;ACTION&gt;Change&lt;/ACTION&gt;
                      &lt;STATUS_HEAD&gt;Z00&lt;/STATUS_HEAD&gt;
                      &lt;CREATIONDATE/&gt;
                      &lt;VBELN&gt;0050023362&lt;/VBELN&gt;
                      &lt;POSNR&gt;000001&lt;/POSNR&gt;
                      &lt;SHIPTO&gt;C001991&lt;/SHIPTO&gt;
                      &lt;QUALITY&gt;C&lt;/QUALITY&gt;
                      &lt;MTSMTO&gt;MTO&lt;/MTSMTO&gt;
                      &lt;INTEXP&gt;IN&lt;/INTEXP&gt;
                      &lt;NTGEW&gt;22.520 &lt;/NTGEW&gt;
                      &lt;GEWEI&gt;TO&lt;/GEWEI&gt;
                      &lt;DELIVERYMODE&gt;Librex&lt;/DELIVERYMODE&gt;
                      &lt;PROPERTIES&gt;
                          &lt;ZPEL_PROPERTY_S&gt;
                              &lt;ID&gt;ACTION&lt;/ID&gt;
                              &lt;DESCRIPTION&gt;Action code&lt;/DESCRIPTION&gt;
                              &lt;DATAVALUE&gt;007&lt;/DATAVALUE&gt;
                              &lt;DATATYPE&gt;CHAR&lt;/DATATYPE&gt;
                              &lt;UM/&gt;
                              &lt;ANY&gt;002&lt;/ANY&gt;
                          &lt;/ZPEL_PROPERTY_S&gt;
                          &lt;ZPEL_PROPERTY_S&gt;
                              &lt;ID&gt;AUGRU&lt;/ID&gt;
                              &lt;DESCRIPTION&gt;Order reason (reason for the business transaction)&lt;/DESCRIPTION&gt;
                              &lt;DATAVALUE&gt;01&lt;/DATAVALUE&gt;
                              &lt;DATATYPE&gt;CHAR&lt;/DATATYPE&gt;
                              &lt;UM/&gt;
                              &lt;ANY/&gt;
                          &lt;/ZPEL_PROPERTY_S&gt;
                          &lt;ZPEL_PROPERTY_S&gt;
                              &lt;ID&gt;ZTERM&lt;/ID&gt;
                              &lt;DESCRIPTION&gt;Terms of payment key&lt;/DESCRIPTION&gt;
                              &lt;DATAVALUE&gt;0401&lt;/DATAVALUE&gt;
                              &lt;DATATYPE&gt;CHAR&lt;/DATATYPE&gt;
                              &lt;UM/&gt;
                              &lt;ANY/&gt;
                          &lt;/ZPEL_PROPERTY_S&gt;
                          &lt;ZPEL_PROPERTY_S&gt;
                              &lt;ID&gt;QUALF&lt;/ID&gt;
                              &lt;DESCRIPTION&gt;IDOC qualifier reference document&lt;/DESCRIPTION&gt;
                              &lt;DATAVALUE&gt;001&lt;/DATAVALUE&gt;
                              &lt;DATATYPE&gt;CHAR&lt;/DATATYPE&gt;
                              &lt;UM/&gt;
                              &lt;ANY&gt;Z022&lt;/ANY&gt;
                          &lt;/ZPEL_PROPERTY_S&gt;
                          &lt;ZPEL_PROPERTY_S&gt;
                              &lt;ID&gt;QUALF&lt;/ID&gt;
                              &lt;DESCRIPTION&gt;IDOC qualifier reference document&lt;/DESCRIPTION&gt;
                              &lt;DATAVALUE&gt;002&lt;/DATAVALUE&gt;
                              &lt;DATATYPE&gt;CHAR&lt;/DATATYPE&gt;
                              &lt;UM/&gt;
                              &lt;ANY&gt;Z023&lt;/ANY&gt;
                          &lt;/ZPEL_PROPERTY_S&gt;
                      &lt;/PROPERTIES&gt;
                  &lt;/item&gt;
              &lt;/DATAAREA&gt;
          &lt;/MSAPO01&gt;
      &lt;/asx:values&gt;
  &lt;/asx:abap&gt;
  </code></pre>
  
  <p>The 'Properties' node is dynamically determined (in numbers and content). My original XSLT is:</p>
  
  <pre><code>&lt;xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.siemens.com/ad/mes/b2mt-1.0" xmlns:amg="Pelican@AMG" xmlns:bml="http://www.wbf.org/xml/b2mml-v02" xmlns:oag="http://www.openapplications.org/oagis"
  xmlns:sit="http://www.siemens.com/ad/mes/b2mml-v02-SITExt-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://www.siemens.com/ad/mes/b2mt-1.0 C:\DATA_EXCHANGE\B2MT\B2MT_Schemas\AMG_ProductionSchedule.xsd"&gt;
    &lt;xsl:template match="MSAPO01"&gt;
      &lt;SyncProductionSchedule&gt;
        &lt;oag:ApplicationArea&gt;
          &lt;oag:Sender&gt;
            &lt;oag:LogicalId&gt;
              &lt;xsl:value-of select="LOGICALID"/&gt;
            &lt;/oag:LogicalId&gt;
            &lt;oag:Component&gt;
              &lt;xsl:value-of select="COMPONENT"/&gt;
            &lt;/oag:Component&gt;
            &lt;oag:Confirmation&gt;
              &lt;xsl:value-of select="CONFIRMATION"/&gt;
            &lt;/oag:Confirmation&gt;
          &lt;/oag:Sender&gt;
          &lt;oag:CreationDateTime&gt;
            &lt;xsl:value-of select="CREATIONDATE"/&gt;
          &lt;/oag:CreationDateTime&gt;
          &lt;oag:BODId&gt;
            &lt;xsl:text&gt;0000000000017092&lt;/xsl:text&gt;
          &lt;/oag:BODId&gt;
        &lt;/oag:ApplicationArea&gt;
        &lt;DataArea&gt;
          &lt;xsl:comment&gt; WorkOrder  &lt;/xsl:comment&gt;
          &lt;oag:Sync&gt;
            &lt;xsl:attribute name="confirm"&gt;
              &lt;xsl:value-of select="SYNC_CONFIRM"/&gt;
            &lt;/xsl:attribute&gt;
            &lt;oag:SyncCriteria&gt;
              &lt;oag:SyncExpression&gt;
                &lt;xsl:attribute name="action"&gt;
                  &lt;xsl:value-of select="SYNC_ACTION"/&gt;
                &lt;/xsl:attribute&gt;
              &lt;/oag:SyncExpression&gt;
            &lt;/oag:SyncCriteria&gt;
          &lt;/oag:Sync&gt;
          &lt;xsl:apply-templates select="DATAAREA"/&gt;
        &lt;/DataArea&gt;
      &lt;/SyncProductionSchedule&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="DATAAREA"&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="DATAAREA/item"&gt;
      &lt;bml:ProductionSchedule&gt;
        &lt;bml:ID&gt;
          &lt;xsl:text&gt;Default&lt;/xsl:text&gt;
        &lt;/bml:ID&gt;
        &lt;bml:ProductionRequest&gt;
          &lt;bml:ID&gt;
            &lt;xsl:value-of select="concat(VBELN,'/',POSNR)"/&gt;
          &lt;/bml:ID&gt;
          &lt;bml:Any&gt;
            &lt;sit:ProductionRequestExtension&gt;
              &lt;sit:Quantity&gt;
                &lt;bml:QuantityString&gt;
                  &lt;xsl:value-of select="NTGEW"/&gt;
                &lt;/bml:QuantityString&gt;
                &lt;bml:DataType&gt;
                  &lt;xsl:text&gt;float&lt;/xsl:text&gt;
                &lt;/bml:DataType&gt;
                &lt;bml:UnitOfMeasure&gt;
                  &lt;xsl:value-of select="GEWEI"/&gt;
                &lt;/bml:UnitOfMeasure&gt;
              &lt;/sit:Quantity&gt;
              &lt;sit:ProducedQuantity&gt;
                &lt;xsl:value-of select="PROD_QTY"/&gt;
              &lt;/sit:ProducedQuantity&gt;
            &lt;/sit:ProductionRequestExtension&gt;
            &lt;amg:ProductionRequestExtension&gt;
              &lt;amg:SAPOrder&gt;
                &lt;amg:SAPOrderID&gt;
                  &lt;xsl:value-of select="VBELN"/&gt;
                &lt;/amg:SAPOrderID&gt;
                &lt;amg:SAPOrderItem&gt;
                  &lt;xsl:value-of select="POSNR"/&gt;
                &lt;/amg:SAPOrderItem&gt;
                &lt;amg:SAPStatusHead&gt;
                  &lt;xsl:value-of select="STATUS_HEAD"/&gt;
                &lt;/amg:SAPStatusHead&gt;              
                &lt;amg:SoldTo&gt;
                  &lt;xsl:value-of select="SOLDTO"/&gt;
                &lt;/amg:SoldTo&gt;
                &lt;amg:SoldToName&gt;
                  &lt;xsl:value-of select="NAME_SOLDTO"/&gt;
                &lt;/amg:SoldToName&gt;
                &lt;amg:ShipTo&gt;
                  &lt;xsl:value-of select="SHIPTO"/&gt;
                &lt;/amg:ShipTo&gt;             
                &lt;amg:Quality&gt;
                  &lt;xsl:value-of select="QUALITY"/&gt;
                &lt;/amg:Quality&gt;
                &lt;amg:DistributionChannel&gt;
                  &lt;xsl:value-of select="INTEXP"/&gt;
                &lt;/amg:DistributionChannel&gt;
                &lt;amg:DeliveryMode&gt;
                  &lt;xsl:value-of select="DELIVERYMODE"/&gt;
                &lt;/amg:DeliveryMode&gt;
              &lt;/amg:SAPOrder&gt;
            &lt;/amg:ProductionRequestExtension&gt;
          &lt;/bml:Any&gt;
        &lt;/bml:ProductionRequest&gt;
      &lt;/bml:ProductionSchedule&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:transform&gt;
  </code></pre>
  
  <p>please help me with the dynamic part in XSLT file in order to get the following output, regardless of the number of properties. The XML output should look like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!-- edited with XMLSpy v2011 rel. 3 sp1 (http://www.altova.com) --&gt;
  &lt;SyncProductionSchedule xmlns="http://www.siemens.com/ad/mes/b2mt-1.0" xmlns:amg="Pelican@ArcelorMittalGalati" xmlns:bml="http://www.wbf.org/xml/b2mml-v02" xmlns:oag="http://www.openapplications.org/oagis" xmlns:sit="http://www.siemens.com/ad/mes/b2mml-v02-SITExt-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;oag:ApplicationArea&gt;
          &lt;oag:Sender&gt;
              &lt;oag:LogicalId&gt;SAP-AMG&lt;/oag:LogicalId&gt;
              &lt;oag:Component&gt;Whatever&lt;/oag:Component&gt;
              &lt;oag:Confirmation&gt;Always&lt;/oag:Confirmation&gt;
          &lt;/oag:Sender&gt;
          &lt;oag:CreationDateTime&gt;2014-02-10T12:52:32&lt;/oag:CreationDateTime&gt;
          &lt;oag:BODId&gt;0000000000017092&lt;/oag:BODId&gt;
      &lt;/oag:ApplicationArea&gt;
      &lt;DataArea&gt;
          &lt;!-- WorkOrder  --&gt;
          &lt;oag:Sync confirm="Never"&gt;
              &lt;oag:SyncCriteria&gt;
                  &lt;oag:SyncExpression action="Change"/&gt;
              &lt;/oag:SyncCriteria&gt;
          &lt;/oag:Sync&gt;
          &lt;bml:ProductionSchedule&gt;
              &lt;bml:ID&gt;Default&lt;/bml:ID&gt;
              &lt;bml:ProductionRequest&gt;
                  &lt;bml:ID&gt;0050023362/000001&lt;/bml:ID&gt;
                  &lt;bml:Any&gt;
                      &lt;sit:ProductionRequestExtension&gt;
                          &lt;sit:Quantity&gt;
                              &lt;bml:QuantityString&gt;22.520 &lt;/bml:QuantityString&gt;
                              &lt;bml:DataType&gt;float&lt;/bml:DataType&gt;
                              &lt;bml:UnitOfMeasure&gt;TO&lt;/bml:UnitOfMeasure&gt;
                          &lt;/sit:Quantity&gt;
                          &lt;sit:ProducedQuantity&gt;0.000 &lt;/sit:ProducedQuantity&gt;
                      &lt;/sit:ProductionRequestExtension&gt;
                      &lt;amg:ProductionRequestExtension&gt;
                          &lt;amg:SAPOrder&gt;
                              &lt;amg:SAPOrderID&gt;0050023362&lt;/amg:SAPOrderID&gt;
                              &lt;amg:SAPOrderItem&gt;000001&lt;/amg:SAPOrderItem&gt;
                              &lt;amg:SAPStatusHead&gt;Z00&lt;/amg:SAPStatusHead&gt;
                              &lt;amg:SoldTo&gt;C001991&lt;/amg:SoldTo&gt;
                              &lt;amg:Quality&gt;C&lt;/amg:Quality&gt;
                              &lt;amg:DistributionChannel&gt;IN&lt;/amg:DistributionChannel&gt;
                              &lt;amg:DeliveryMode&gt;Librex&lt;/amg:DeliveryMode&gt;
                              &lt;!-- New Node - OrderProperty -&gt; Begin  --&gt;
                              &lt;amg:SAPOrderProperties&gt;
                                  &lt;amg:OrderProperty&gt;
                                      &lt;amg:ID&gt;ACTION&lt;/amg:ID&gt;
                                      &lt;amg:Description&gt;Action code&lt;/amg:Description&gt;
                                      &lt;amg:Value&gt;
                                          &lt;amg:ValueString&gt;007&lt;/amg:ValueString&gt;
                                          &lt;amg:DataType&gt;CHAR&lt;/amg:DataType&gt;
                                          &lt;amg:UnitOfMeasure&gt;n/a&lt;/amg:UnitOfMeasure&gt;
                                          &lt;amg:Any&gt;002&lt;/amg:Any&gt;
                                      &lt;/amg:Value&gt;
                                  &lt;/amg:OrderProperty&gt;
                                  &lt;amg:OrderProperty&gt;
                                      &lt;amg:ID&gt;AUGRU&lt;/amg:ID&gt;
                                      &lt;amg:Description&gt;Order reason (reason for the business transaction)&lt;/amg:Description&gt;
                                      &lt;amg:Value&gt;
                                          &lt;amg:ValueString&gt;01&lt;/amg:ValueString&gt;
                                          &lt;amg:DataType&gt;CHAR&lt;/amg:DataType&gt;
                                          &lt;amg:UnitOfMeasure&gt;n/a&lt;/amg:UnitOfMeasure&gt;
                                          &lt;amg:Any/&gt;
                                      &lt;/amg:Value&gt;
                                  &lt;/amg:OrderProperty&gt;
                                  &lt;amg:OrderProperty&gt;
                                      &lt;amg:ID&gt;ZTERM&lt;/amg:ID&gt;
                                      &lt;amg:Description&gt;Terms of payment key&lt;/amg:Description&gt;
                                      &lt;amg:Value&gt;
                                          &lt;amg:ValueString&gt;0401&lt;/amg:ValueString&gt;
                                          &lt;amg:DataType&gt;CHAR&lt;/amg:DataType&gt;
                                          &lt;amg:UnitOfMeasure&gt;n/a&lt;/amg:UnitOfMeasure&gt;
                                          &lt;amg:Any/&gt;
                                      &lt;/amg:Value&gt;
                                  &lt;/amg:OrderProperty&gt;
                                  &lt;amg:OrderProperty&gt;
                                      &lt;amg:ID&gt;QUALF&lt;/amg:ID&gt;
                                      &lt;amg:Description&gt;IDOC qualifier reference document&lt;/amg:Description&gt;
                                      &lt;amg:Value&gt;
                                          &lt;amg:ValueString&gt;001&lt;/amg:ValueString&gt;
                                          &lt;amg:DataType&gt;CHAR&lt;/amg:DataType&gt;
                                          &lt;amg:UnitOfMeasure&gt;n/a&lt;/amg:UnitOfMeasure&gt;
                                          &lt;amg:Any&gt;Z022&lt;/amg:Any&gt;
                                      &lt;/amg:Value&gt;
                                  &lt;/amg:OrderProperty&gt;
                                  &lt;amg:OrderProperty&gt;
                                      &lt;amg:ID&gt;QUALF&lt;/amg:ID&gt;
                                      &lt;amg:Description&gt;IDOC qualifier reference document&lt;/amg:Description&gt;
                                      &lt;amg:Value&gt;
                                          &lt;amg:ValueString&gt;001&lt;/amg:ValueString&gt;
                                          &lt;amg:DataType&gt;CHAR&lt;/amg:DataType&gt;
                                          &lt;amg:UnitOfMeasure&gt;n/a&lt;/amg:UnitOfMeasure&gt;
                                          &lt;amg:Any&gt;Z023&lt;/amg:Any&gt;
                                      &lt;/amg:Value&gt;
                                  &lt;/amg:OrderProperty&gt;
                              &lt;/amg:SAPOrderProperties&gt;
                              &lt;!-- New Node - OrderProperty -&gt; End --&gt;
                          &lt;/amg:SAPOrder&gt;
                      &lt;/amg:ProductionRequestExtension&gt;
                  &lt;/bml:Any&gt;
              &lt;/bml:ProductionRequest&gt;
          &lt;/bml:ProductionSchedule&gt;
      &lt;/DataArea&gt;
  &lt;/SyncProductionSchedule&gt;
  </code></pre>