Count-Elements.xsl

From TEIWiki
Jump to navigation Jump to search

Summary

This is a short stylesheet which simply counts the elements used in a document and outputs this as the contents of a <tagsDecl> 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 <tagsDecl> element, suitable for cutting and pasting into the teiHeader of the document. As an 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>

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()"/>
 <tagUsage gi="{name(.)}"><xsl:value-of select="count(key('gis', name(.)))" /></tagUsage>
</xsl:for-each>
</tagsDecl>
</xsl:template>

</xsl:stylesheet>