2

Playing around in order to learn XSLT, I have the following XML file and XSLT stylesheet. I want to produce a list of the players ranked after the score 3*goals + 2*assists.

<?xml-stylesheet type="text/xsl" href="player_ranking.xsl"?>
<soccer xmlns="http://mysoccer.it"> 
  <players>
    <player>
      <name>Pelé</name>
      <goals>21</goals>
      <assists>9</assists>
    </player>
    <player>
      <name>Beckenbauer</name>
        <goals>7</goals>
        <assists>18</assists>
      </player>
      <player>
        <name>Spiderman</name>
        <goals>27</goals>
        <assists>38</assists>
      </player>
      <player>
        <name>Hagi</name>
        <goals>13</goals>
        <assists>14</assists>
      </player>
      <player>
        <name>Laudrup</name>
        <goals>11</goals>
        <assists>25</assists>
      </player>
      <player>
        <name>Gullit</name>
        <goals>17</goals>
        <assists>15</assists>
      </player>
    </players>
  </soccer>

Style sheet

 <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:soc="http://mysoccer.it"
     xmlns="http://www.w3.org/1999/xhtml">

   <xsl:template match="soc:soccer">
     <html>
       <head>
    <title>Famous players ranked</title>
       </head>
       <body>
         <h1>Overall ranking</h1>
         <xsl:apply-templates match="soc:players"/>
       </body>
     </html>
   </xsl:template>

   <xsl:template match="soc:players">
     <xsl:apply-templates select="soc:player">
       <xsl:sort select="soc:goals * 3 + 2 * soc:assists" order="descending"/>
     </xsl:apply-templates>
   </xsl:template>

   <xsl:template match="soc:player">
     <xsl:value-of select="soc:name"/> 
     with score
     <xsl:value-of select="soc:goals * 3 + 2 * soc:assists"/>
     <br/>
   </xsl:template>

 </xsl:stylesheet>

In a browser (tried with both Opera and Firefox) it produces

 Overall ranking

 Laudrup with score 83
 Pelé with score 81
 Gullit with score 81
 Hagi with score 67
 Beckenbauer with score 57
 Spiderman with score 157

Spiderman should appear on top of the list. Can someone point out my error ?

I have used version="2.0" in the style sheet as using version="1.0" gives me Error loading stylesheet: Parsing an XSLT stylesheet failed.

1 Answer 1

3

Looks like its doing an alphanumeric sort, so 1 < 5 etc

Try changing the sort line to:

<xsl:sort select="soc:goals * 3 + 2 * soc:assists" data-type="number" order="descending"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.