Count-Elements.xsl
Jump to navigation
Jump to search
This is a short stylesheet which simply counts the elements used in a document and outputs this as the contents of a <tagsDecl> element.
For example, if you run this script on itself, you get:
<?xml version="1.0" encoding="UTF-8"?> <tagsDecl> <tagUsage gi="tagsDecl">1</tagUsage> <tagUsage gi="tagUsage">1</tagUsage> <tagUsage gi="xsl:for-each">1</tagUsage> <tagUsage gi="xsl:key">1</tagUsage> <tagUsage gi="xsl:output">1</tagUsage> <tagUsage gi="xsl:sort">1</tagUsage> <tagUsage gi="xsl:stylesheet">1</tagUsage> <tagUsage gi="xsl:template">1</tagUsage> <tagUsage gi="xsl:value-of">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()"/>
<tagUsage gi="{name(.)}"><xsl:value-of select="count(key('gis', name(.)))" /></tagUsage>
</xsl:for-each>
</tagsDecl>
</xsl:template>
</xsl:stylesheet>