<xsl:stylesheet xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" version="1.0">
The XSL declaration with the namespace, this is clear, I quess.
<xsl

utput indent="yes"/>
Advises the XSL Processor to indent output tags to make them somewhat more readable.
<xsl:key name="pageKey" match="//HierarchyMember" use="@PageKey"/>
Creates a key (think of an associative array) named pageKey from all HierarchyMember elements anywhere in the document, using their PageKey attribute as association.
<xsl:template match="/">
<Root>
<xsl:apply-templates select="//HierarchyMember"/>
</Root>
</xsl:template>
Matches the root node, puts a Root element around the output and begins processing all HierarchyMember elements in the documant one by one.
The next template is executed once per HierarchyMember element:
<xsl:template match="HierarchyMember">
<xsl:if test="count(key('pageKey', @PageKey)) > 1">
------------------------------------------------^
This should be escaped with >
Tests if there is more than one HierarchyMember element with a PageKey attribute equal to the currently processed HierarchyMember. The key function returns all node of the previously created key that have the value of the current PageKey attribute associated with them.
If this is true, copy the current element into the output:
....but add two attributes - one containing the current PageKey:
<xsl:attribute name="PageKey">
<xsl:value-of-select="@PageKey"/>
</xsl:attribute>
....and one containing the count of the HierarchyMember elements havin the same PageKey as the current one:
<xsl:attribute name="Count">
<xsl:value-of select="count(key 'pageKey',@PageKey))"/> </xsl:attribute> </xsl:copy> </xsl:if>
End if.
<xsl:apply-templates select="HierarchyMember"/>
This is senseless. The template is already repeatedly called by the one that matches the document root.
End of HierarchyMember template.
End of stylesheet.
What the styleheet does is counting how many HierarchyMember elements with duplicate PageKeys exist in the source .
I don't know if it is a bug, but I guess: The xsl:if will cause duplicate HierarchyMember elemets to appear in the output, as may as there are duplicate ones in the input. That means, if there are two HierarchyMember's having PageKey="1", the outpot will be
<HierarchyMember PageKey="1" Count="2" />
<HierarchyMember PageKey="1" Count="2" />
because there are two elements passing the "count(key('pageKey', @PageKey)) > 1" test, which are processed one by one, so the condition is true twice during the transformation process.
Martin