home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeXSLSearch this book

D.3. How Do I List All the Elements in an XML Document?

As in our last example, this job is for generic XPath expressions. We'll use the grouping techniques described in Section 6.2, "Grouping Nodes" in Chapter 6, "Sorting and Grouping Elements", along with the name() function, to accomplish this. Our stylesheet sorts all element names alphabetically, and then groups them to list each unique element once, along with a count of how many times that element appears:

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

  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable>

  <xsl:key name="elements" match="*" use="name()"/>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:text>Summary of Elements</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:value-of select="$newline"/>
    <xsl:for-each 
      select="//*[generate-id(.)=generate-id(key('elements',name())[1])]">
      <xsl:sort select="name()"/>
      <xsl:for-each select="key('elements', name())">
        <xsl:if test="position()=1">
          <xsl:text>Element </xsl:text>
          <xsl:value-of select="name()"/>
          <xsl:text> occurs </xsl:text>
          <xsl:value-of select="count(//*[name()=name(current())])"/>
          <xsl:text> times.</xsl:text>
          <xsl:value-of select="$newline"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
    <xsl:value-of select="$newline"/>
    <xsl:text>There are </xsl:text>
    <xsl:value-of select="count(//*)"/>
    <xsl:text> elements in all.</xsl:text>
  </xsl:template>
    
</xsl:stylesheet>

When we run this stylesheet against the XML source file for Appendix C, "XSLT and XPath Function Reference", here are the results:


Summary of Elements

Element appendix occurs 1 times.
Element emphasis occurs 9 times.
Element filename occurs 11 times.
Element funcdef occurs 36 times.
Element funcprototype occurs 36 times.
Element funcsynopsis occurs 36 times.
Element function occurs 181 times.
Element graphic occurs 8 times.
Element itemizedlist occurs 9 times.
Element link occurs 1 times.
Element listitem occurs 52 times.
Element literal occurs 14 times.
Element para occurs 338 times.
Element paramdef occurs 45 times.
Element programlisting occurs 110 times.
Element quote occurs 4 times.
Element refentry occurs 36 times.
Element refname occurs 36 times.
Element refnamediv occurs 36 times.
Element refpurpose occurs 36 times.
Element refsect1 occurs 144 times.
Element refsynopsisdiv occurs 36 times.
Element literal occurs 184 times.
Element term occurs 13 times.
Element title occurs 146 times.
Element variablelist occurs 2 times.
Element varlistentry occurs 13 times.
Element xref occurs 14 times.

There are 1587 elements in all.

This stylesheet works against any valid XML document, regardless of the elements that document uses. For more information on the technique we used to group the element names (popularly known as the Muench method), see Section 6.2, "Grouping Nodes" in Chapter 6, "Sorting and Grouping Elements".



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.