Q:

XSL Header and Detail Grouping

I am trying to create a group of xml from a file that contains multiple header rows followed by multiple detail rows for each header. 
Here is an example of the original XML:
EDIT: The number of Detail sheets that follow the Header sheets will vary. So, Sheet[4]/FIELD1 will not always have the value of Header. Also, the file will always have one or more Header sheets. Sheets are rows.
EDIT: XSLT-1.0
<Root>
    <Sheet>
        <FIELD1>Header</FIELD1>
        <FIELD2>Value1</FIELD2>
        <FIELD3>Value2</FIELD3>
    </Sheet>
    <Sheet>
         <FIELD1>Detail</FIELD1>
         <FIELD2>Value3</FIELD2>
         <FIELD3>Value4</FIELD3>
      </Sheet>
     <Sheet>
          <FIELD1>Detail</FIELD1>
          <FIELD2>Value5</FIELD2>
          <FIELD3>Value6</FIELD3>
      </Sheet>
     <Sheet>
          <FIELD1>Header</FIELD1>
          <FIELD2>Value7</FIELD2>
          <FIELD3>Value8</FIELD3>
      </Sheet>
     <Sheet>
          <FIELD1>Detail</FIELD1>
          <FIELD2>Value9</FIELD2>
          <FIELD3>Value10</FIELD3>
      </Sheet>
     <Sheet>
          <FIELD1>Detail</FIELD1>
          <FIELD2>Value11</FIELD2>
          <FIELD3>Value12</FIELD3>
      </Sheet>
     <Sheet>
          <FIELD1>Detail</FIELD1>
          <FIELD2>Value13</FIELD2>
          <FIELD3>Value14</FIELD3>
    </Sheet>
 </Root>

Here is what the output should resemble:
EDIT: The number of invoices will be determined by the number of Header sheets(rows). The LineItem rows will be the same number of Detail sheets that follow the Header.
<Root>
    <Invoice AttribOne="Value1" AttribTwo="Value2" >
        <LineItem LIAttribOne="Value3" LIAttribTwo="Value4"/>
        <LineItem LIAttribOne="Value5" LIAttribTwo="Value6"/>
    </Invoice>
    <Invoice AttribOne="Value7" AttribTwo="Value8" >
        <LineItem LIAttribOne="Value9" LIAttribTwo="Value10"/>
        <LineItem LIAttribOne="Value11" LIAttribTwo="Value12"/>
        <LineItem LIAttribOne="Value13" LIAttribTwo="Value14"/>
    </Invoice>
</Root>

EDIT: Answered below:

A:

I have worked this out and have added comments in the code below to describe it.
Here is the solution:
<xsl:template match="/Root">
    <Root>
         <xsl:apply-templates select="Sheet[FIELD1 = 'Header']" />
    </Root>
</xsl:template>

<xsl:template match="Sheet[FIELD1 = 'Detail']" >
    <LineItem>
      <xsl:attribute name="LIAttribOne" >
        <xsl:value-of select="FIELD2"/>
      </xsl:attribute>
      <xsl:attribute name="LIAttribTwo" >
        <xsl:value-of select="FIELD3"/>
      </xsl:attribute>
    </LineItem>
</xsl:template>

<xsl:template  match="Sheet[FIELD1 = 'Header']">
    <Invoice>
      <xsl:attribute name="AttribOne" >
        <xsl:value-of select="FIELD2"/>
      </xsl:attribute>
      <xsl:attribute name="AttribTwo" >
        <xsl:value-of select="FIELD3"/>
      </xsl:attribute>
  <!-- Get the count of all details the previous headers contained -->
  <xsl:variable name="prevCnt" select="count(preceding-sibling::*[FIELD1 = 'Detail'])"/>
  <!-- Get the count of all details in the previous headers and this header contain -->
  <xsl:variable name="newCnt" select="count(following-sibling::*[FIELD1 = 'Header'][1]/preceding-sibling::*[FIELD1 = 'Detail'])"/>
  <!-- This will return a 1 if there is a following header, else 0 -->
  <xsl:variable name="pos" select="count(following-sibling::*[FIELD1 = 'Header'][1])" />
  <!-- select every detail whose position is less than $newCnt - $preCnt or all if there is no following header -->
  <xsl:apply-templates select="following-sibling::*[FIELD1 = 'Detail'][position() &lt; ($newCnt - $prevCnt + 1) or $pos = 0]"   />
</Invoice>

