title: Find xml element using xslt
body: <p>I'm using xslt to convert some xml files. My input data looks like this:</p>
  
  <pre><code>&lt;data&gt;
    &lt;dataItem&gt;
      &lt;value&gt;24014&lt;/value&gt;
      &lt;date&gt;Feb 11, 2013&lt;/date&gt;
    &lt;/dataItem&gt;
    &lt;dataItem&gt;
      &lt;value&gt;0&lt;/value&gt;
      &lt;date&gt;Feb 12, 2013&lt;/date&gt;
    &lt;/dataItem&gt;
    &lt;dataItem&gt;
      &lt;value&gt;0&lt;/value&gt;
      &lt;date&gt;Feb 13, 2013&lt;/date&gt;
    &lt;/dataItem&gt;
    &lt;dataItem&gt;
      &lt;value&gt;24627&lt;/value&gt;
      &lt;date&gt;Feb 14, 2013&lt;/date&gt;
    &lt;/dataItem&gt;
    &lt;dataItem&gt;
      &lt;value&gt;0&lt;/value&gt;
      &lt;date&gt;Feb 15, 2013&lt;/date&gt;
    &lt;/dataItem&gt;
  &lt;/data&gt;
  </code></pre>
  
  <p>My output data needs to look like this:</p>
  
  <pre><code>&lt;root&gt;
    &lt;item&gt;
      &lt;text&gt;
        Feb 14, 2013
      &lt;/text&gt;
    &lt;/item&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>In other words I'm looking for the most recent date element where the value element is not 0. The dataItem elements are guarantee to be in increasing date order (starting at the top of the document).</p>
  
  <p>I've tried to approach this recursively, like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:output method="xml" indent="yes"/&gt;
  
    &lt;xsl:template name="findDate"&gt;
      &lt;xsl:param name="list"/&gt;
      &lt;xsl:choose&gt;
  
        &lt;xsl:when test="value = 0"&gt;
          &lt;xsl:call-template name="findDate"&gt;
            &lt;xsl:with-param name="list" select="$list[position()!=1]"/&gt;
          &lt;/xsl:call-template&gt;
        &lt;/xsl:when&gt;
  
        &lt;xsl:otherwise&gt;
          &lt;root&gt;
            &lt;item&gt;
              &lt;text&gt;
                &lt;xsl:value-of select="label"/&gt;
              &lt;/text&gt;
            &lt;/item&gt;
          &lt;/root&gt;
        &lt;/xsl:otherwise&gt;
  
      &lt;/xsl:choose&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="/"&gt;
        &lt;xsl:call-template name="findDate"&gt;
          &lt;xsl:with-param name="list" select="//dataItem"/&gt;
        &lt;/xsl:call-template&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>So far though, it's not working, and all I'm getting out is this:</p>
  
  <pre><code>   &lt;root&gt;
        &lt;item&gt;
          &lt;text&gt;
          &lt;/text&gt;
        &lt;/item&gt;
      &lt;/root&gt;
  </code></pre>
  
  <p>Can anyone see where I'm going wrong?</p>
  
  <p>Thanks,</p>
  
  <p>Paul</p>