Count-Elements.xsl

This is a short stylesheet which simply counts the elements used in a document and outputs this as the contents of a  element.

For example, if you run this script on itself, you get:   1 1 1 1 1 1 1</tagUsage> 1</tagUsage> 1</tagUsage> </tagsDecl>

But that is correct because there is only one instance of each element in the script itself:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/>

<xsl:key name="gis" match="*" use="name"/>

<xsl:template match="node"> <tagsDecl> <xsl:for-each select="//*[generate-id(.)=generate-id(key('gis',name(.))[1])]"> <xsl:sort select="name"/> <xsl:value-of select="count(key('gis', name(.)))" /></tagUsage> </xsl:for-each> </tagsDecl> </xsl:template>

</xsl:stylesheet>