p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

xslt thread: Solution To My Previous "Problems"


Message #1 by "Stephen Walsh" <doctor_bad_guy@h...> on Thu, 13 Jun 2002 02:32:10
I listed a problem a few weeks ago and got some help from a few people on 
here.  This is a thank you note, THANK YOU!  I finished it 2 weeks ago but 
was to lazy to write it up on here.  Just a reference note for others for 
counting attributes and that sort of gear.

Steve

Here is the Problem:

Convert This:

<?xml version="1.0"?>
<Match>
  <Date>21/3/2005</Date><Stadium>Wembley</Stadium>
  <Team Name="Liverpool">
    <Goal Scorer="O'Reilly" Time="15"/>
    <Goal Scorer="Smith"    Time="20"/>
    <Goal Scorer="O'Reilly" Time="57"/>
    <Goal Scorer="Jones"    Time="65"/>
    <Goal Scorer="Smith"    Time="78"/>
    <Goal Scorer="O'Reilly" Time="88"/>
  </Team>
  <Team Name="Real Madrid">
    <Goal Scorer="Charles"  Time="48"/>
    <Goal Scorer="Humble"   Time="55"/>
    <Goal Scorer="Santana"  Time="85"/>
  </Team>
</Match>

To This:

Scorer Goals 
O'Reilly 3 
Smith    2 
Jones    1 


Here is the solution.

<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">
<xsl:output method="html" /> 
<xsl:param name="Team-Name" /> 
<xsl:key name="Team_Name" match="Team" use="@Name" /> 
<xsl:template match="Match">
<HTML>
<TITLE>Goal Scorers</TITLE> 
<BODY STYLE="margin-left: 20px; margin-right: 20px; background-color: 
#FFFFBB; color:maroon; font-family:'Comic Sans MS'; font-size:12pt; 
padding:0px 6px">
<TABLE>
<TR>
<TD>
<B>Scorer</B> 
</TD>
<TD>
<B>Goals</B> 
</TD>
</TR>
<xsl:apply-templates select="key('Team_Name',$Team-Name)" /> 
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="Team">
<xsl:for-each select="Goal[not(@Scorer=preceding-sibling::Goal/@Scorer)]">
<xsl:sort select="count(//Goal[@Scorer=current()/@Scorer])" 
order="descending" /> 
<xsl:sort select="@Scorer" /> 
<TR>
<TD>
<xsl:value-of select="@Scorer" /> 
</TD>
<TD align="Center">
<xsl:value-of select="count(//Goal[@Scorer=current()/@Scorer])" /> 
</TD>
</TR>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

  Return to Index