[Solved] How to apply XSLT transformations to XML file and produce another XML?


The following XSLT does what you need (except formatting):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes" encoding="iso-8859-1"/>

    <!-- Copy all elements recursively -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Copy this element with subelements -->
    <xsl:template match="order">
        <!-- Save ID for queries -->
        <xsl:variable name="id" select="id" />
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
          <!-- Find all quantity elements by ID -->
          <xsl:for-each select="//quantity[id_order=$id]">
              <!-- Create element with name set to value of unit element and value of value element -->
              <xsl:element name="{unit}">
                  <xsl:value-of select="value"/>              
              </xsl:element>
          </xsl:for-each>
        </xsl:copy>
    </xsl:template>

    <!-- Skip these elements -->
    <xsl:template match="quantities" />
    <xsl:template match="date" />
</xsl:stylesheet>

RESULT

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <orders>
      <order>
         <id>1</id>
         <number>10002</number>
         <type>Loading</type>
         <KG>1000</KG>
      </order>
      <order>
         <id>2</id>
         <number>10003</number>
         <type>Loading</type>
         <PAL>3</PAL>
      </order>
   </orders>
</output>

solved How to apply XSLT transformations to XML file and produce another XML?