<xslTutorial creator="nicmila@idoox.com">
<index keywords='count() position() descendant name() current()'/>

<description><stylesheet id='id2'/> and  <stylesheet id='id3'/> count elements which occured in XML source. <stylesheet id='id4'/> lists all children of each element in the source. 
</description>

<xmlSource id="id1">
<AAA>
  <BBB id="b1">
     <CCC name="q" size="12"/>
           <EEE id="e1">
              <SSS/>
          </EEE>
     <CCC weight="45"/>
     <CCC/>
  </BBB>
</AAA>
<AAA>
  <EEE id="e2"/>
  <CCC>
    <DDD/>
    <DDD/>
  </CCC>
</AAA>
</xmlSource>

<attValues>
<value match="">
</value>
</attValues>

<xslStylesheet id="id2">
<xsl:template match="/">

<TABLE border='1' width="90%">
<TR><TH>Element</TH><TH>Occurs</TH></TR>
<xsl:for-each select="/descendant::*">
<xsl:variable name="pos" select="position()"/>
<xsl:if test="not(/descendant::*[position() < $pos and name()=name(current())])">
<TR><TD><xsl:value-of select="name()"/></TD><TD>
<xsl:value-of select="count(/descendant::*[name()=name(current())])"/>
</TD></TR>
</xsl:if>
</xsl:for-each>
<TR><TH>Total count</TH><TH><xsl:value-of select="count(//*)"/></TH></TR>
</TABLE>
</xsl:template>
</xslStylesheet>

<xslStylesheet id="id3">
<xsl:template match="/">
<TABLE border='1' width="90%">
<xsl:apply-templates select="//*"/>
<TR><TH>Total count</TH><TH><xsl:value-of select="count(//*)"/></TH></TR>
</TABLE>
</xsl:template>

<xsl:template match="*">
<xsl:if test="not(descendant::*[name()=name(current())] | following::*[name()=name(current())])">
<TR><TD><xsl:value-of select="name()"/></TD>
<TD><xsl:value-of select="count(/descendant::*[name()=name(current())])"/></TD>
</TR>
</xsl:if>
</xsl:template>


</xslStylesheet>

<xslStylesheet id="id4">
<xsl:template match="/">
<xsl:for-each select="/descendant::*">
<xsl:variable name="pos" select="position()"/>
<xsl:variable name="name" select="name()"/>
<xsl:if test="not(/descendant::*[position() < $pos and name()= $name])">
<P><xsl:value-of select="$name"/>
<xsl:text> [</xsl:text>
<xsl:for-each select="//*[name()=$name]/*">
<xsl:variable name="p" select="position()"/>
<xsl:if test="not(//*[name()=$name]/*[position()< $p and name()=name(current())])">
<xsl:value-of select="name()"/><xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</P>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xslStylesheet>

<xslStylesheet id="id5">
<xsl:template match="/">
<TABLE border='1' width="90%">
<xsl:apply-templates select="//*"/>
</TABLE>
</xsl:template>

<xsl:template match="*">
<xsl:if test="not(descendant::*[name()=name(current())] | following::*[name()=name(current())])">
<TR><TD><xsl:value-of select="name()"/></TD>
<TD><xsl:apply-templates select="." mode='list'/></TD>
</TR>
</xsl:if>
</xsl:template>

<xsl:template match="*" mode='list'>

<xsl:for-each select="//*[name()=name(current())]/*">
<xsl:value-of select="name()"/>
</xsl:for-each>
</xsl:template>
</xslStylesheet>



</xslTutorial>