Skip to content

Instantly share code, notes, and snippets.

@greystate
Created December 6, 2010 15:17
Show Gist options
  • Save greystate/730418 to your computer and use it in GitHub Desktop.
Save greystate/730418 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8" ?>
<!--
Assumes an XML document with multiple <order> elements inside a wrapping <orders> element.
-->
<!DOCTYPE xsl:stylesheet [
<!-- Close your eyes and take a leap of faith! -->
<!ENTITY countAllOccurences "sum(ancestor::orders/order/orderLine[properties/productNumber = current()/following-sibling::productNumber]/@quantity)">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<!-- Find orderLine elements by the productNumber they contain -->
<xsl:key name="orderLine-by-productNumber" match="orderLine" use="properties/productNumber" />
<xsl:template match="orders">
<ol>
<xsl:apply-templates select="order/orderLine[count(. | key('orderLine-by-productNumber', properties/productNumber)[1]) = 1]/properties/productName">
<xsl:sort select="&countAllOccurences;" data-type="number" order="descending" />
</xsl:apply-templates>
</ol>
</xsl:template>
<xsl:template match="productName">
<li>
<xsl:value-of select="." /> (<xsl:text />
<xsl:value-of select="&countAllOccurences;" />)<xsl:text />
</li>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment