XSLT in XML

<!-- Some template that's called in the context of the record parent-->
<xsl:template name="main">
  <!-- Call the grouping template passing all of the records-->
  <xsl:call-template name="group">
    <xsl:with-param name="records" select="record"/>
  </xsl:call-template>
</xsl:template>

<!-- Group the records-->
<xsl:template name="group">
  <xsl:param name="records"/>
  <!-- The purchaseOrder value of the first record in the group-->
  <xsl:variable name="purchaseOrder"
    select="$records[position()  = 1]/column[@name = 'purchaseOrder']"/>
  <!--
    This is the start of a group so apply some sort of
    groupHeader template to the first record
  -->
  <xsl:apply-templates select="$records[position() = 1]" mode="groupHeader"/>
  <!-- Apply some sort of groupBody template to all of the records-->
  <xsl:apply-templates select="$records[column[@name = 'purchaseOrder'] = $purchaseOrder]"
    mode="groupBody"/>
  <!-- Remove all of the records that have been processed from the list-->
  <xsl:variable name="remainingRecords"
    select="$records[column[@name = 'purchaseOrder']  != $purchaseOrder]"/>
  <!-- If there are any records left call ourselves with the new shorter list-->
  <xsl:if test="$remainingRecords">
    <xsl:call-template name="group">
      <xsl:with-param name="records" select="$remainingRecords"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>