It's a bit late and I had a beer or two, but I think this will work for you.
My additions to your XSL is in
bold.
="1.0"
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:key name="level0" match="//product" use="@ordercat"/>
<xsl:template match="/">
<data>
<xsl:attribute name="type">K8 order creation control file</xsl:attribute>
<process><xsl:value-of select="count(/order/productgroup/supplier/product[count(. | key('level0', @ordercat)[1]) = 1])"/></process>
<xsl:for-each select="/order/productgroup/supplier/product[count(. | key('level0', @ordercat)[1]) = 1]">
<order>
<xsl:attribute name="number"><xsl:number format="1" value="position()"/></xsl:attribute>
<xsl:variable name="onrOrdCat" select="@ordercat" />
<orderCategory><xsl:value-of select="$onrOrdCat"/></orderCategory>
<orderLines>
<xsl:apply-templates select="../product[@ordercat = $onrOrdCat]" />
</orderLines>
</order>
</xsl:for-each>
</data>
</xsl:template>
<xsl:template match="product">
<line>
<xsl:attribute name="what"><xsl:value-of select="@what" /></xsl:attribute>
<xsl:attribute name="pos"><xsl:value-of select="@pos" /></xsl:attribute>
</line>
</xsl:template>
</xsl:stylesheet>
The output is as follows:
<data type="K8 order creation control file">
<process>3</process>
<order number="1">
<ordercategory>C</ordercategory>
<orderlines>
<line what="1" pos="1" />
<line what="1" pos="2" />
<line what="1" pos="3" />
<line what="1" pos="4" />
<line what="1" pos="6" />
<line what="1" pos="7" />
<line what="1" pos="8" />
<line what="1" pos="9" />
<line what="1" pos="10" />
<line what="1" pos="11" />
<line what="1" pos="12" />
<line what="1" pos="13" />
<line what="1" pos="14" />
<line what="1" pos="15" />
</orderlines>
</order>
<order number="2">
<ordercategory>S</ordercategory>
<orderlines>
<line what="1" pos="5" />
</orderlines>
</order>
<order number="3">
<ordercategory>N</ordercategory>
<orderlines>
<line what="3" pos="1" />
<line what="3" pos="2" />
<line what="3" pos="3" />
<line what="3" pos="4" />
<line what="3" pos="5" />
<line what="3" pos="6" />
</orderlines>
</order>
</data>
You can of course change the output to your preference, but I think I got the information right.