• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<xsl:transform
2 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 version="1.0"
4>
5
6  <!-- Test FileName: mk048.xsl -->
7  <!-- Source Attribution:
8       This test was written by Michael Kay and is taken from
9       'XSLT Programmer's Reference' published by Wrox Press Limited in 2000;
10       ISBN 1-861003-12-9; copyright Wrox Press Limited 2000; all rights reserved.
11       Now updated in the second edition (ISBN 1861005067), http://www.wrox.com.
12       No part of this book may be reproduced, stored in a retrieval system or
13       transmitted in any form or by any means - electronic, electrostatic, mechanical,
14       photocopying, recording or otherwise - without the prior written permission of
15       the publisher, except in the case of brief quotations embodied in critical articles or reviews.
16  -->
17  <!-- Example: soccer.xml, league.xsl -->
18  <!-- Chapter/Page: 7-521 -->
19  <!-- Purpose: Creates scorecard for soccer league using count() and sum() functions -->
20
21<xsl:variable name="teams" select="//team[not(.=preceding::team)]"/>
22<xsl:variable name="matches" select="//match"/>
23
24<xsl:template match="results">
25<html><body>
26   <h1>Results of Group <xsl:value-of select="@group"/></h1>
27
28   <table cellpadding="5">
29      <tr>
30        <td>Team</td>
31        <td>Played</td>
32        <td>Won</td>
33        <td>Drawn</td>
34        <td>Lost</td>
35        <td>For</td>
36        <td>Against</td>
37     </tr>
38   <xsl:for-each select="$teams">
39        <xsl:variable name="this" select="."/>
40        <xsl:variable name="played" select="count($matches[team=$this])"/>
41        <xsl:variable name="won"
42            select="count($matches[team[.=$this]/@score &gt; team[.!=$this]/@score])"/>
43        <xsl:variable name="lost"
44            select="count($matches[team[.=$this]/@score &lt; team[.!=$this]/@score])"/>
45        <xsl:variable name="drawn"
46            select="count($matches[team[.=$this]/@score = team[.!=$this]/@score])"/>
47        <xsl:variable name="for"
48            select="sum($matches/team[.=current()]/@score)"/>
49        <xsl:variable name="against"
50            select="sum($matches[team=current()]/team/@score) - $for"/>
51
52        <tr>
53        <td><xsl:value-of select="."/></td>
54        <td><xsl:value-of select="$played"/></td>
55        <td><xsl:value-of select="$won"/></td>
56        <td><xsl:value-of select="$drawn"/></td>
57        <td><xsl:value-of select="$lost"/></td>
58        <td><xsl:value-of select="$for"/></td>
59        <td><xsl:value-of select="$against"/></td>
60        </tr>
61   </xsl:for-each>
62   </table>
63</body></html>
64</xsl:template>
65
66
67  <!--
68   * Licensed to the Apache Software Foundation (ASF) under one
69   * or more contributor license agreements. See the NOTICE file
70   * distributed with this work for additional information
71   * regarding copyright ownership. The ASF licenses this file
72   * to you under the Apache License, Version 2.0 (the  "License");
73   * you may not use this file except in compliance with the License.
74   * You may obtain a copy of the License at
75   *
76   *     http://www.apache.org/licenses/LICENSE-2.0
77   *
78   * Unless required by applicable law or agreed to in writing, software
79   * distributed under the License is distributed on an "AS IS" BASIS,
80   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81   * See the License for the specific language governing permissions and
82   * limitations under the License.
83  -->
84
85</xsl:transform>
86