RE: Creating statistics for a given XML instance

Subject: RE: Creating statistics for a given XML instance
From: Kay Michael <Michael.Kay@xxxxxxx>
Date: Fri, 22 Sep 2000 14:18:40 +0100
A Saxon soslution:

<?xml version="1.0"?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0"
  xmlns:saxon="http://icl.com/saxon"; >

<!-- Test saxon:distinct() -->

<xsl:output indent="yes"/>

<xsl:variable name="nameexp" select="saxon:expression('name()')"/>
<xsl:variable name="attnames" select="saxon:distinct($allatts, $nameexp)"/>
<xsl:variable name="elnames" select="saxon:distinct($allelements,
$nameexp)"/>
<xsl:variable name="allatts" select="//@*"/>
<xsl:variable name="allelements" select="//*"/>

<xsl:template match="/">

<table>
<tr><td>Element</td><td>Total</td>
<xsl:for-each select="$attnames">
   <td><xsl:value-of select="name()"/></td>
</xsl:for-each>
</tr>
<xsl:for-each select="$elnames">
<tr>
  <xsl:variable name="elname" select="name()"/>
  <td><xsl:value-of select="$elname"/></td>
  <td><xsl:value-of select="count($allelements[name()=$elname])"/></td>
  <xsl:for-each select="$attnames">
     <xsl:variable name="attname" select="name()"/>
     <td><xsl:value-of 
           select="count($allatts[name()=$attname and
name(..)=$elname])"/></td>
  </xsl:for-each>
</tr>
</xsl:for-each>
</table>

</xsl:template>
 
</xsl:stylesheet>

When applied to itself the output is:

<?xml version="1.0" encoding="utf-8"?>
<table xmlns:saxon="http://icl.com/saxon";>
   <tr>
      <td>Element</td>
      <td>Total</td>
      <td>version</td>
      <td>name</td>
      <td>select</td>
      <td>match</td>
   </tr>
   <tr>
      <td>xsl:stylesheet</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
   </tr>
   <tr>
      <td>xsl:variable</td>
      <td>7</td>
      <td>0</td>
      <td>7</td>
      <td>7</td>
      <td>0</td>
   </tr>
   <tr>
      <td>xsl:template</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
   </tr>
   <tr>
      <td>table</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
   </tr>
   <tr>
      <td>tr</td>
      <td>2</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
   </tr>
   <tr>
      <td>td</td>
      <td>6</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
   </tr>
   <tr>
      <td>xsl:value-of</td>
      <td>4</td>
      <td>0</td>
      <td>0</td>
      <td>4</td>
      <td>0</td>
   </tr>
   <tr>
      <td>xsl:for-each</td>
      <td>3</td>
      <td>0</td>
      <td>0</td>
      <td>3</td>
      <td>0</td>
   </tr>
</table>

In standard XSLT, it's much harder, though no doubt possible.

Mike Kay

> -----Original Message-----
> From: Wegmann, Frank [mailto:Frank.Wegmann@xxxxxxxxxxxxxx]
> Sent: 22 September 2000 11:20
> To: 'xsl-list@xxxxxxxxxxxxxxxx'
> Subject: Creating statistics for a given XML instance
> 
> 
> 
> I wonder if it is possible with XSL to create a statistics for
> a given XML file that tells which element has been used how often
> along with the usage of every attribute.
> 
> 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread