Count-Elements.xsl

Summary
This is a short stylesheet which simply counts the elements used in a document and outputs this as the contents of a  element. See also Generate tagsDecl P5.xslt.

Required Input
This stylesheet will take any well-formed XML file as its input.

Expected Output
The output is provided as a  element, suitable for cutting and pasting into the teiHeader of the document. As an example, if you run this script on itself, you get:

  1 1 1 1 1 1 1</tagUsage> 1</tagUsage> 1</tagUsage> </tagsDecl>

Known Restrictions or Problems
You may not wish to be counting elements in the teiHeader of the document you are applying this to. You should be able to avoid this by adding the following to the stylesheet: <xsl:template match="teiHeader"/>

Stylesheet
There is only one instance of each element in the script itself, so that count above is actually correct

<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>