title: xml transformation with xsl-beginner
body: <p>I want to transform a simple xml using xsl. I know there are many examples on this site, but if anyone can help me go to the next step. </p>
  
  <p>This is the source xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;output&gt;
    &lt;cars&gt;
      &lt;car&gt;
        &lt;id&gt;1&lt;/id&gt;
        &lt;brand&gt;Audi&lt;/brand&gt;
        &lt;type&gt;A3&lt;/type&gt;
        &lt;license&gt;TST&lt;/license&gt;
      &lt;/car&gt;
    &lt;/cars&gt;
    &lt;distances&gt;
      &lt;distance&gt;
        &lt;id_car&gt;1&lt;/id_car&gt;
        &lt;date&gt;20110901&lt;/date&gt;
        &lt;distance&gt;111&lt;/distance&gt;
      &lt;/distance&gt;
      &lt;distance&gt;
        &lt;id_car&gt;1&lt;/id_car&gt;
        &lt;date&gt;20110902&lt;/date&gt;
        &lt;distance&gt;23&lt;/distance&gt;
      &lt;/distance&gt;
      &lt;distance&gt;
        &lt;id_car&gt;1&lt;/id_car&gt;
        &lt;date&gt;20110903&lt;/date&gt;
        &lt;distance&gt;10&lt;/distance&gt;
      &lt;/distance&gt;
    &lt;/distances&gt;
  &lt;/output&gt;
  </code></pre>
  
  <hr>
  
  <p>XSL result will be this (results) :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;output&gt;
    &lt;cars&gt;
      &lt;car&gt;
        &lt;id&gt;1&lt;/id&gt;
        &lt;brand&gt;Audi&lt;/brand&gt;
        &lt;type&gt;A3&lt;/type&gt;
        &lt;license&gt;TST&lt;/license&gt;
        &lt;distances&gt;
          &lt;distance day="20110901"&gt;111&lt;/distance&gt;
          &lt;distance day="20110902"&gt;23&lt;/distance&gt;
          &lt;distance day="20110903"&gt;10&lt;/distance&gt;
        &lt;/distances&gt;
      &lt;/car&gt;
    &lt;/cars&gt;
  </code></pre>
  
  <h2>    </h2>
  
  <p>Tthis is the last xsl. Something is missing, because it does not work.</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
  &lt;xsl:key name="Dist_car" match="distances/distance" use="id_car" /&gt;
  &lt;xsl:template match="car"&gt;
  &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@*|node()"/&gt;
          &lt;distances&gt;    
                 &lt;xsl:apply-templates select="key('Dist_car', id)"/&gt;
           &lt;/distances&gt;
  &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="distances"&gt;
          &lt;distance day="{date}"&gt;&lt;xsl:value-of select="distance"/&gt;&lt;/distance&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>