title: xslt how to select either the current node or a preceding sibling but not both
body: <p>So I have xml that look like this</p>
  
  <pre><code>&lt;Data&gt;
      &lt;History&gt;
          &lt;z r="1" c1="BILL" c2="123" c3="02/20/2006" c4="100.00" c5=".00" c6=".00" c7="100.00" c8=".00" c9=".00" c10=".00" lcdesc="BILL"/&gt;
          &lt;z r="2" c1="PAYCODE" c2="456" c3="03/16/2006" c4="-100.00" c5=".00" c6=".00" c7="-100.00" c8=".00" c9=".00" c10=".00" lcdesc="PAYMENT"/&gt;
          &lt;z r="3" c1="Total" c2="123" c3="02/20/2006" c4=".00" c5=".00" c6=".00" c7=".00" c8=".00" c9=".00" c10=".00" lcdesc="Total"/&gt;
          &lt;z r="4" lcdesc=""/&gt;
          &lt;z r="5" c1="BILL" c2="124" c3="03/27/2006" c4="13000.00" c5="400.00" c6=".00" c7="13400.00" c8=".00" c9=".00" c10=".00" lcdesc="BILL"/&gt;
          &lt;z r="6" c1="PAYCODE" c2="457" c3="04/13/2006" c4="-13000.00" c5="-400.00" c6=".00" c7="-13400.00" c8=".00" c9=".00" c10=".00" lcdesc="PAYMENT"/&gt;
          &lt;z r="7" c1="Total" c2="124" c3="03/27/2006" c4=".00" c5=".00" c6=".00" c7=".00" c8=".00" c9=".00" c10=".00" lcdesc="Total"/&gt;
          &lt;z r="8" lcdesc=""/&gt;
          &lt;z r="9" c1="BILL" c2="125" c3="04/28/2006" c4="1000.00" c5=".00" c6=".00" c7="1000.00" c8=".00" c9=".00" c10=".00" lcdesc="BILL"/&gt;
          &lt;z r="10" c1="PAYCODE" c2="458" c3="05/10/2006" c4="-1000.00" c5=".00" c6=".00" c7="-1000.00" c8=".00" c9=".00" c10=".00" lcdesc="PAYMENT"/&gt;
          &lt;z r="11" c1="Total" c2="125" c3="04/28/2006" c4=".00" c5=".00" c6=".00" c7=".00" c8=".00" c9=".00" c10=".00" lcdesc="Total"/&gt;
          &lt;z r="12" lcdesc=""/&gt;
     &lt;/History&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>And I want to put some information from the c1="BILL" z node on the subsequent lines.  I want to get the invoice number (c2) and the invoice date (c3).  So I figured in my template I'd declare a variable and do a select for either the current node if c1 = 'BILL' or the preceding-sibling where c1 = 'BILL'.  While it works for the first node and it's subsequent lines, on the second bill line it puts the previous bill lines information.  So something isn't right.  Can you help?</p>
  
  <p>Here is my non-working xslt:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"&gt;
  &lt;xsl:output method="xml" omit-xml-declaration="yes"/&gt;
      &lt;xsl:template match="*|/"&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="text()|@*"&gt;
          &lt;xsl:value-of select="."/&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:output indent="no"/&gt;
          &lt;xsl:template match="/"&gt;
  &lt;History&gt;
                  &lt;xsl:apply-templates select="//History/z"/&gt;
  &lt;/History&gt;
      &lt;/xsl:template&gt;
  
  
      &lt;xsl:template match="/PageData/History/z"&gt;
  &lt;xsl:variable name="invnode" select="current()[@c1 = 'BILL'] | preceding-sibling::z[@c1 = 'BILL'][1]" /&gt;
          &lt;z&gt;
              &lt;xsl:for-each select="@*"&gt;
                  &lt;!--get the attribute name--&gt;
                  &lt;xsl:variable name="nodename"&gt;
                      &lt;xsl:value-of select="name()"/&gt;
                  &lt;/xsl:variable&gt;
                  &lt;xsl:attribute name="{$nodename}"&gt;&lt;xsl:value-of select="."/&gt;&lt;/xsl:attribute&gt;
              &lt;/xsl:for-each&gt;
                          &lt;xsl:attribute name="invoice"&gt;&lt;xsl:value-of select="$invnode/@c2" /&gt;&lt;/xsl:attribute&gt;
                          &lt;xsl:if test="string-length($invnode/@c3) &gt;= 10"&gt;
                                  &lt;xsl:attribute name="invdate1"&gt;&lt;xsl:value-of select="concat(substring($invnode/@c3, 7), substring($invnode/@c3, 1, 2), substring($invnode/@c3, 4, 2))" /&gt;&lt;/xsl:attribute&gt;
                                  &lt;xsl:attribute name="invdate2"&gt;&lt;xsl:value-of select="concat(substring($invnode/@c3, 7), '-', substring($invnode/@c3, 1, 2), '-', substring($invnode/@c3, 4, 2))" /&gt;&lt;/xsl:attribute&gt;
                          &lt;/xsl:if&gt;
          &lt;/z&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The output I want is as follows:</p>
  
  <pre><code>&lt;Data&gt;
     &lt;History&gt;
        &lt;z r="1" c1="BILL" c2="123" c3="02/20/2006" c4="100.00" c5=".00" c6=".00" c7="100.00" c8=".00" c9=".00" c10=".00" lcdesc="BILL" invoice="123" invdate1="20060220" invdate2="2006-02-20" /&gt;
        &lt;z r="2" c1="PAYCODE" c2="456" c3="03/16/2006" c4="-100.00" c5=".00" c6=".00" c7="-100.00" c8=".00" c9=".00" c10=".00" lcdesc="PAYMENT"  invoice="123" invdate1="20060220" invdate2="2006-02-20" /&gt;
        &lt;z r="3" c1="Total" c2="123" c3="02/20/2006" c4=".00" c5=".00" c6=".00" c7=".00" c8=".00" c9=".00" c10=".00" lcdesc="Total" invoice="123" invdate1="20060220" invdate2="2006-02-20" /&gt;
        &lt;z r="4" lcdesc="" invoice="123" invdate1="20060220" invdate2="2006-02-20" /&gt;
        &lt;z r="5" c1="BILL" c2="124" c3="03/27/2006" c4="13000.00" c5="400.00" c6=".00" c7="13400.00" c8=".00" c9=".00" c10=".00" lcdesc="BILL"  invoice="124" invdate1="20060327" invdate2="2006-03-27" /&gt;
        &lt;z r="6" c1="PAYCODE" c2="457" c3="04/13/2006" c4="-13000.00" c5="-400.00" c6=".00" c7="-13400.00" c8=".00" c9=".00" c10=".00" lcdesc="PAYMENT" invoice="124" invdate1="20060327" invdate2="2006-03-27" /&gt;
        &lt;z r="7" c1="Total" c2="124" c3="03/27/2006" c4=".00" c5=".00" c6=".00" c7=".00" c8=".00" c9=".00" c10=".00" lcdesc="Total" invoice="124" invdate1="20060327" invdate2="2006-03-27" /&gt;
        &lt;z r="8" lcdesc="" invoice="124" invdate1="20060327" invdate2="2006-03-27" /&gt;
        &lt;z r="9" c1="BILL" c2="125" c3="04/28/2006" c4="1000.00" c5=".00" c6=".00" c7="1000.00" c8=".00" c9=".00" c10=".00" lcdesc="BILL" invoice="125" invdate1="20060428" invdate2="2006-04-28" /&gt;
        &lt;z r="10" c1="PAYCODE" c2="458" c3="05/10/2006" c4="-1000.00" c5=".00" c6=".00" c7="-1000.00" c8=".00" c9=".00" c10=".00" lcdesc="PAYMENT" invoice="125" invdate1="20060428" invdate2="2006-04-28" /&gt;
        &lt;z r="11" c1="Total" c2="125" c3="04/28/2006" c4=".00" c5=".00" c6=".00" c7=".00" c8=".00" c9=".00" c10=".00" lcdesc="Total" invoice="125" invdate1="20060428" invdate2="2006-04-28" /&gt;
        &lt;z r="12" lcdesc="" invoice="125" invdate1="20060428" invdate2="2006-04-28" /&gt;
     &lt;/History&gt;
  &lt;/Data&gt;
  </code></pre>