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.


Here is the Problem:

Convert This:

<?xml version="1.0"?>
  <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 Name="Real Madrid">
    <Goal Scorer="Charles"  Time="48"/>
    <Goal Scorer="Humble"   Time="55"/>
    <Goal Scorer="Santana"  Time="85"/>

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" 
<xsl:output method="html" /> 
<xsl:param name="Team-Name" /> 
<xsl:key name="Team_Name" match="Team" use="@Name" /> 
<xsl:template match="Match">
<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">
<xsl:apply-templates select="key('Team_Name',$Team-Name)" /> 
<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" /> 
<xsl:value-of select="@Scorer" /> 
<TD align="Center">
<xsl:value-of select="count(//Goal[@Scorer=current()/@Scorer])" /> 

  Return to Index