Q:

How to transform nested xml table to flat table with xslt

I have an xml file 
<xml>
  <order>
    <orderid>123</orderid>
    <orderdate>2013-04-12T00:00:00.000+01:00</orderdate>
    <orderline>
      <articlename>AAAA</articlename>
      <quantity>10</quantity>
    </orderline>
    <orderline>
      <articlename>BBBB</articlename>
      <quantity>15</quantity>
    </orderline>
  </order>
  <order>
    ...
    </order>
</xml>

And i need to transform it using XSLT to:
<xml>
  <item>
    <orderid>123</orderid>
    <articlename>AAAA</articlename>
    <quantity>10</quantity>
  </item>
  <item>
    <orderid>123</orderid>
    <articlename>BBBB</articlename>
    <quantity>15</quantity>
  </item>
</xml>

I already tried some transformations using <xsl:call-template> and <xsl:apply-templates>, but nothing worked for me.
Thanks in advance.

A:

This simple stylesheet should do it:
Stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!--
  Identity transform:
  http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT 
  -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="orderline">
    <item>
      <!--
      Apply preceding <orderid> sibling and the children of the current element
      -->
      <xsl:apply-templates select="preceding-sibling::orderid | node()"/>
    </item>
  </xsl:template>

  <xsl:template match="order">
    <!--
    Only apply <orderline> children, <orderdate> is dropped and <orderid> is
    handled by the template above
    -->
    <xsl:apply-templates select="orderline"/>
  </xsl:template>
</xsl:stylesheet>

Input
<xml>
  <order>
    <orderid>123</orderid>
    <orderdate>2013-04-12T00:00:00.000+01:00</orderdate>
    <orderline>
      <articlename>AAAA</articlename>
      <quantity>10</quantity>
    </orderline>
    <orderline>
      <articlename>BBBB</articlename>
      <quantity>15</quantity>
    </orderline>
  </order>

  <!-- Added additional <order> element for demonstration -->
  <order>
    <orderid>456</orderid>
    <orderdate>2014-05-13T00:00:00.000+02:00</orderdate>
    <orderline>
      <articlename>CCCC</articlename>
      <quantity>20</quantity>
    </orderline>
    <orderline>
      <articlename>DDDD</articlename>
      <quantity>25</quantity>
    </orderline>
  </order>
</xml>

Output
<?xml version="1.0" encoding="utf-8"?>
<xml>
  <item>
    <orderid>123</orderid>
    <articlename>AAAA</articlename>
    <quantity>10</quantity>
  </item>
  <item>
    <orderid>123</orderid>
    <articlename>BBBB</articlename>
    <quantity>15</quantity>
  </item>
  <item>
    <orderid>456</orderid>
    <articlename>CCCC</articlename>
    <quantity>20</quantity>
  </item>
  <item>
    <orderid>456</orderid>
    <articlename>DDDD</articlename>
    <quantity>25</quantity>
  </item>
</xml>

