title: xslt <key use="position()" ../> problem
body: <p>I've got a (very excel spreadsheet like..) xml data, from which I'm supposed to create a more readable one. <br />
  I have the headers on the top of the structure, and would like to create <code>elements</code> from their text value and apply them on the rest of the document.</p>
  
  <p>Probably the actual data speaks clearer, so my input document looks like</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;root&gt;
      &lt;headers&gt;
          &lt;header&gt;line&lt;/header&gt;
          &lt;header&gt;product&lt;/header&gt;
          &lt;header&gt;order&lt;/header&gt;
          &lt;header&gt;qty&lt;/header&gt;
          &lt;header&gt;deadline&lt;/header&gt;
      &lt;/headers&gt;
      &lt;row&gt;
          &lt;data&gt;2&lt;/data&gt;
          &lt;data&gt;HU12_SETUP&lt;/data&gt;
          &lt;data&gt;16069061&lt;/data&gt;
          &lt;data&gt;1&lt;/data&gt;
          &lt;data&gt;2011-04-13T09:22:59.980&lt;/data&gt;
      &lt;/row&gt;
      &lt;row&gt;
          &lt;data&gt;1&lt;/data&gt;
          &lt;data&gt;40PFL7605H/12&lt;/data&gt;
          &lt;data&gt;16310360&lt;/data&gt;
          &lt;data&gt;200&lt;/data&gt;
          &lt;data&gt;2011-04-13T09:22:59.980&lt;/data&gt;
      &lt;/row&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>and my goal is to have an xml document as:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;morning&gt;
      &lt;row&gt;
          &lt;line&gt;2&lt;/line&gt;
          &lt;product&gt;HU12_SETUP&lt;/product&gt;
          &lt;order&gt;16069061&lt;/order&gt;
          &lt;qty&gt;1&lt;/qty&gt;
          &lt;deadline&gt;0&lt;/deadline&gt;
      &lt;/row&gt;
      &lt;row&gt;
          &lt;line&gt;1&lt;/line&gt;
          &lt;product&gt;40PFL7605H/12&lt;/product&gt;
          &lt;order&gt;16310360&lt;/order&gt;
          &lt;qty&gt;200&lt;/qty&gt;
          &lt;deadline&gt;77&lt;/deadline&gt;
      &lt;/row&gt;
  &lt;/morning&gt;
  </code></pre>
  
  <p>I'd like to do this in a "proper" / "efficient" way, that's why I'm turning to you, guys, to help me out. <br />
  I figured using a <code>key</code> to match <code>data</code> positions to <code>header</code> positions would be my solution, but for some reason it just doesn't work (I'am already ~X( ). </p>
  
  <p>What I need is to point out what is wrong with my xsl, and/or if there's something wrong with the <code>key</code> concept, advise me better solution. </p>
  
  <p>Here is my (debugging) xsl: </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="xml" encoding="UTF-8" indent="yes"/&gt;
      &lt;xsl:strip-space elements="*"/&gt;
      &lt;xsl:key name="header" match="header" use="position()" /&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;morning&gt;
              &lt;xsl:apply-templates /&gt;
          &lt;/morning&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="headers" /&gt;
      &lt;xsl:template match="row"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates /&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="data"&gt;
          &lt;xsl:element name="{concat('bla-',position())}"&gt;
              &lt;xsl:value-of select="key('header',position())" /&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>where I verify that the position() is actually correct. </p>
  
  <p>My output varies depending on which stylesheet version I use. </p>
  
  <p><strong>output 1.0</strong>:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;morning&gt;
    &lt;row&gt;
      &lt;bla-1&gt;line&lt;/bla-1&gt;
      &lt;bla-2/&gt;
      &lt;bla-3/&gt;
      &lt;bla-4/&gt;
      &lt;bla-5/&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;bla-1&gt;line&lt;/bla-1&gt;
      &lt;bla-2/&gt;
      &lt;bla-3/&gt;
      &lt;bla-4/&gt;
      &lt;bla-5/&gt;
    &lt;/row&gt;
  &lt;/morning&gt;
  </code></pre>
  
  <p><strong>output 2.0</strong>:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;morning&gt;
    &lt;row&gt;
      &lt;bla-1&gt;line product order qty deadline&lt;/bla-1&gt;
      &lt;bla-2/&gt;
      &lt;bla-3/&gt;
      &lt;bla-4/&gt;
      &lt;bla-5/&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;bla-1&gt;line product order qty deadline&lt;/bla-1&gt;
      &lt;bla-2/&gt;
      &lt;bla-3/&gt;
      &lt;bla-4/&gt;
      &lt;bla-5/&gt;
    &lt;/row&gt;
  &lt;/morning&gt;
  </code></pre>
  
  <p>As you can see, <code>key('header',position())</code> gives me empty string in all the cases except the first (that's why i have it as value, not element name). </p>
  
  <p>I would appreciate any help, thank you in advance!</p>
  
  <p><strong>Addition</strong>: <br />
  Based on @LarsH's and @Alejandro's answer (still stuck to this <code>key</code> thing...) I came up with: </p>
  
  <pre><code>&lt;xsl:template match="data"&gt;
      &lt;xsl:variable name="posn" select="position()" /&gt;
      &lt;xsl:element name="{key('header',1)[$posn]}" /&gt;
      &lt;xsl:element name="{key('header',1)[position()]}" /&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>I can see that using a key here with a static <code>1</code> is dumb, but I'm getting scared why the two resulting elements from up there do not match? <br />
  The first is correct, the second always gives me <code>line</code>, respectively <code>lineproductorderqtydeadline</code> back.</p>
  
  <p>Could anyone point me to the right direction?</p>