[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] find first occurrence of attribute value grouped by ele ment


Subject: RE: [xsl] find first occurrence of attribute value grouped by ele ment
From: John Pallister <jpallister@xxxxxxxxxxxx>
Date: Fri, 27 Sep 2002 15:00:20 -0400

My thanks to Andrew and James for their help with this problem.

There are probably more elegant ways to code this, but this is the completed
XSLT solution which I have implemented (posted to assist any fellow newbies
that may be interested). 
 
XSLT file:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="html"/>
<xsl:key name="keyTest" match="test" use="@id" />
<xsl:key name="keyPassed" match="test[@passed='T']" use="@id" />
<xsl:key name="keyFailed" match="test[@passed='F']" use="@id" />
 
<xsl:variable name="thetests">
 <xsl:for-each select="//test"> 
      <xsl:variable name="id" select="@id"/>   
        <atest id="{@id}" Unit_id="{parent::testdata/@Unit_id}">  
        <xsl:attribute name="pft">
         <xsl:choose> 
          <xsl:when test="@passed='T' and
not(preceding-sibling::test[@id=$id]/@passed='F')">yes</xsl:when>
          <xsl:otherwise>no</xsl:otherwise>
         </xsl:choose>
        </xsl:attribute>   
       </atest>
   </xsl:for-each>
</xsl:variable>

<xsl:key name="keyPFT" match="atest[@pft='yes']" use="@id" />

<xsl:template match="/">
	<html>
	 <head><title>Summary Report</title></head>
	 <body>
	  <table border="1">
	   <tr>
	    <th>Test</th>
	    <th>Total Attempts</th>
	    <th>Total Pass</th>
	    <th>Total Fail</th>
	    <th>Pass on First Attempt</th>
	    <th>First Run Ratio</th>
	   </tr>
	   <testdata>
	    <xsl:call-template name="summary_list"/>
           </testdata>
	  </table>
	</body>
   </html>
</xsl:template>


<xsl:template match="//testdata/test[generate-id(.) =
generate-id(key('keyTest', @id))]"  name="summary_list">  
 <xsl:for-each select = "//testdata/test[@id and generate-id(.) =
generate-id(key('keyTest', @id))]" >
  <tr>   
   <!-- Display the Test Name!  -->
   <td align="center"><b><xsl:value-of select = "@id" /></b></td>
					
   <!-- Count all tests!  -->
   <td align="center"><xsl:value-of select = "count(key('keyTest',@id))"
/></td>
   <xsl:variable name="tc" select="count(key('keyTest',@id))"/>    	
			
   <!-- Count the passes!  -->
   <td align="center"><xsl:value-of select = "count(key('keyPassed',@id))"
/></td>
      	 
   <!-- Count the fails!  -->
   <td align="center"><xsl:value-of select = "count(key('keyFailed',@id))"
/></td>   
   
   <!-- Count the first run pass! --> 
   <td align="center">
    <xsl:variable name="id" select="@id"/>   
    <xsl:if test="not(msxsl:node-set($thetests)/atest[generate-id(.) =
generate-id(key('keyPFT', $id))])">
     <xsl:value-of  select = "'0'" />
     <td align="right"><xsl:value-of  select = "'0%'" /></td> 
    </xsl:if> 

   <xsl:for-each select = "msxsl:node-set($thetests)/atest[generate-id(.) =
generate-id(key('keyPFT', $id))]">    
      <xsl:value-of select = "count(key('keyPFT',$id))" />
        <xsl:if test="count(key('keyPFT',@id))">
       
    <!-- Calculate First Run Ratio -->
         <td align="right">
          <xsl:value-of  select = "format-number(count(key('keyPFT',@id))
div $tc,'###.###%')" />
         </td> 
        </xsl:if>  
     </xsl:for-each>          
    </td>     	 
  </tr>   
 </xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Thanks Again!
John Pallister
jpallister@xxxxxxxxxxxx


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



Current Thread
Keywords