XSLTXT code for grouping using recursion. Syntax highlighting is provided by XSLTXT mode in emacs.

# Some template that's called in the context of the record parent
tpl .name "main"
  # Call the grouping template passing all of the records
  call "group" ("records":"record")

# Group the records
tpl .name "group" ("records")
  # The purchaseOrder value of the first record in the group
  var "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
  apply .select "$records[position() = 1]" .mode "groupHeader"
  # Apply some sort of groupBody template to all of the records
  apply .select "$records[column[@name = 'purchaseOrder'] = $purchaseOrder]"
    .mode "groupBody"
  # Remove all of the records that have been processed from the list
  var "remainingRecords"
    .select "$records[column[@name = 'purchaseOrder']  != $purchaseOrder]"
   # If there are any records left call ourselves with the new shorter list
  if "$remainingRecords"
    call "group" ("records":"$remainingRecords")