Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • J 4 posts 24 karma points
    Apr 22, 2010 @ 03:57
    J
    0

    How do I load all data in an XML feed then sort and limit.

    Hey all I'm not a noobie but please treat me as one.

    I am running our local baseball leagues website and we receive stats via XML to our website. I have been using XLST to display those stats creatively with a leaders board. Basically I am highlighting the top guy in a table then displaying another table right below it with 5 rows and sorting those rows. You can take a look here www.nycmbl.com. The thing is that the code I have written is literally only reading the first 5 entries of data in the XML and then sorts those. What I am trying to do is to get the XSLT code to read all the sorce XML data then sort that data and then list the top person then the top 5. 

    I am at a loss and wracking my brains. Is this possible Can anyone help? My code is below.  

     

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

     <xsl:output method="html" encoding="iso-8859-1" indent="no"/>
    <xsl:template match="stats">

    <style type="text/css">
    table.ex1 {border-spacing: 0}
    table.ex1 td, th {padding: 0 0.2em}
    table.ex1 tr:nth-child(odd) {color: #000; background: #ECF6FC}
    table.ex1 tr:nth-child(even) {color: #000; background:#FFFFFF }
    </style> 

    <table border="1" width="200" style="border-collapse: collapse" bordercolor="#808080" >

    <tr bgcolor="#808080">
            <th align="center"><b><font face="Arial" size="4" color="#FFFFFF">RBI</font></b></th>
          </tr>

    </table>
    <table border="1" width="200" style="border-collapse: collapse" bordercolor="#808080" >
                <xsl:for-each select="rbi/batter[position() &lt; 2]">

    <tr bgcolor="#FFFFFF">
            <th width="75" align="center" height="70" style="border-right-style: none; border-right-width: medium"><b><font face="Arial" size="1">RBI</font><br></br><font face="Arial" size="5" color="#FF0000"><xsl:value-of select="rbi"/></font></b></th>
            <th width="125" align="center" height="70" style="border-left-style: none; border-left-width: medium"><b><font face="Arial" size="2">#<xsl:value-of select="jersey"/><br></br><xsl:value-of select="playername"/><br></br></font><font face="Arial" size="2" color="#808080"><xsl:value-of select="teamname"/></font></b></th>
          </tr>
          </xsl:for-each>
    </table>

        <table class="ex1" border="1" width="200" style="border-collapse: collapse" bordercolor="#808080" >
          <tr bgcolor="#808080">
            <th bgcolor="#808080"><b><font face="Arial" size="1" color="#FFFFFF">#</font></b></th>
            <th bgcolor="#808080"><b><font face="Arial" size="1" color="#FFFFFF">BATTER</font></b></th>
            <th bgcolor="#808080"><b><font face="Arial" size="1" color="#FFFFFF">TEAM</font></b></th>
            <th bgcolor="#808080"><b><font face="Arial" size="1" color="#FFFFFF">RBI</font></b></th>
          </tr>
          <xsl:for-each select="rbi/batter[position() &lt; 6]">

          <xsl:sort select="rbi" lang="language-code" data-type="number" order="descending" case-order="upper-first|lower-first"/>
    <tr>
            <td align="center" width="30"><font face="Arial" size="1" color="#000000"><xsl:value-of select="jersey"/></font></td>
            <td align="center" width="100"><font face="Arial" size="1" color="#000000"><xsl:value-of select="playername"/></font></td>
            <td align="center" width="35"><font face="Arial" size="1" color="#000000"><xsl:value-of select="teamname"/></font></td>
            <td align="center" width="35"><font face="Arial" size="1" color="#000000"><xsl:value-of select="rbi"/></font></td>
          </tr>
          </xsl:for-each>
    </table></xsl:template>
     
    </xsl:stylesheet>



  • Peter Dijksterhuis 1442 posts 1722 karma points
    Apr 22, 2010 @ 08:55
    Peter Dijksterhuis
    0

    Hi,

    you could try to get the position out of the xpath like this:

    <xsl:for-each select="rbi/batter">
    <xsl:sort select="rbi" lang="language-code" data-type="number" order="descending" case-order="upper-first|lower-first"/> 
    <xsl:if test="position() &lt; 6">
    <tr>
    <td align="center" width="30"><font face="Arial" size="1" color="#000000"><xsl:value-of select="jersey"/></font></td>
    <td align="center" width="100"><font face="Arial" size="1" color="#000000"><xsl:value-of select="playername"/></font></td>
    <td align="center" width="35"><font face="Arial" size="1" color="#000000"><xsl:value-of select="teamname"/></font></td>
    <td align="center" width="35"><font face="Arial" size="1" color="#000000"><xsl:value-of select="rbi"/></font></td>
    </tr>
    </xsl:if>
    </xsl:for-each>

    HTH,

    Peter

  • J 4 posts 24 karma points
    Apr 22, 2010 @ 18:05
    J
    0

    Thanks Peter that worked!

  • Eran 292 posts 436 karma points
    Apr 25, 2010 @ 13:45
    Eran
    0

    complexity question:

    in thesituation, when the position is bigger then 6 :

    <xsl:if test="position() &lt; 6">

    it stop looping the items and exit the for-each, or the loop continue to check the next items?

    Eran.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies