Difference between revisions of "Count-Elements.xsl"

From TEIWiki
Jump to navigation Jump to search
m (*doh* forgot to put it in its category!)
m (added headings)
Line 1: Line 1:
 +
== Summary ==
 
This is a short stylesheet which simply counts the elements used in a document and outputs this as the contents of a  
 
This is a short stylesheet which simply counts the elements used in a document and outputs this as the contents of a  
 
<nowiki><tagsDecl></nowiki> element.
 
<nowiki><tagsDecl></nowiki> element.
  
For example, if you run this script on itself, you get:
+
== Required Input ==
 +
 
 +
This stylesheet will take any well-formed XML file as its input.
 +
 
 +
== Expected Output ==
 +
 
 +
The output is provided as a <nowiki><tagsDecl></nowiki> element, suitable for
 +
cutting and pasting into the teiHeader of the document.  As an example, if  
 +
you run this script on itself, you get:
 +
 
 
<pre>
 
<pre>
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
Line 18: Line 28:
 
</pre>
 
</pre>
  
But that is correct because there is only one instance of each element in the script itself:
+
== 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:
 +
<pre>
 +
<xsl:template match="teiHeader"/>
 +
</pre>
 +
 
 +
 
 +
== Stylesheet ==
 +
There is only one instance of each element in the script itself, so that count above is actually correct
  
 
<pre>
 
<pre>

Revision as of 16:28, 21 April 2005

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.

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>