[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Re: Using XSL for a "world records" table

Subject: Re: Re: Using XSL for a "world records" table
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sun, 18 May 2003 10:51:10 +0200
current time xsl
Sorry, I'm still sleepy this Sunday morning.

Use the following transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>

  <xsl:key name="kByTime" match="record"
  use="time"/>

  <xsl:template match="/">
    <xsl:for-each
     select="/*/record[generate-id()
                      =
                       generate-id(key('kByTime',
                                        time
                                       )[1]
                                   )
                       ]">
       <xsl:sort select="time" data-type="number"/>
       <xsl:variable name="groupPos"
       select="1 + count(/*/record[time &lt; current()/time])"/>

       <xsl:for-each select="key('kByTime', time)">
         <xsl:sort select="@date" data-type="number"/>
         <xsl:text>&#xA;</xsl:text>
         <xsl:value-of select="$groupPos"/>
         <xsl:copy-of select="."/>
       </xsl:for-each>
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

When applied on this source.xml:

<records>
 <record name="abc" date="20011130">
   <time>12</time>
 </record>
 <record name="abc" date="19991130">
   <time>12</time>
 </record>
 <record name="abc" date="20011130">
   <time>18</time>
 </record>
 <record name="abc" date="20011130">
   <time>11</time>
 </record>
 <record name="abc" date="19991130">
   <time>11</time>
 </record>
 <record name="abc" date="20011130">
   <time>22</time>
 </record>
 <record name="abc" date="20011130">
   <time>15</time>
 </record>
</records>

The wanted result is produced:


1<record name="abc" date="19991130">
   <time>11</time>
 </record>
1<record name="abc" date="20011130">
   <time>11</time>
 </record>
3<record name="abc" date="19991130">
   <time>12</time>
 </record>
3<record name="abc" date="20011130">
   <time>12</time>
 </record>
5<record name="abc" date="20011130">
   <time>15</time>
 </record>
6<record name="abc" date="20011130">
   <time>18</time>
 </record>
7<record name="abc" date="20011130">
   <time>22</time>
 </record>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"Dimitre Novatchev" <dnovatchev@xxxxxxxxx> wrote in message
news:ba7flb$344$1@xxxxxxxxxxxxxxxxx
> > Thanks Dimitre. It almost works with grouping, but now the record in 3rd
> > place is listed as 2nd place. That is:
> >
> > Place   Time         Name
> > 1       11 seconds   Person 1
> > 1 11 seconds   Person 2
> > 2       12 seconds   Person 3
> >
> > My code looks like this:
> >
> > <xsl:for-each select="record[count(. | key('records-by-time', time)[1])
=
> 1]">
> >  <xsl:sort data-type="number" select="time"/>
> >  <xsl:apply-templates select="key('records-by-time', time)">
> >   <xsl:with-param name="place" select="position()"/>
> >  </xsl:apply-templates>
> > </xsl:for-each>
> >
> > As you see, I'm using the value of position() for place.
> >
> > Do you know of any way to get around this?
>
> Hi Ryan,
>
> Use something like this:
>
> <xsl:stylesheet version="1.0"
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>  <xsl:output omit-xml-declaration="yes"/>
>
>   <xsl:key name="kByTime" match="record"
>   use="time"/>
>
>   <xsl:template match="/">
>     <xsl:for-each
>      select="/*/record[generate-id()
>                       =
>                        generate-id(key('kByTime',
>                                         time
>                                        )[1]
>                                    )
>                        ]">
>        <xsl:sort select="time" data-type="number"/>
>        <xsl:for-each select="key('kByTime', time)">
>          <xsl:sort select="@date" data-type="number"/>
>          <xsl:text>&#xA;</xsl:text>
>          <xsl:value-of select="position() + count(/*/record[time &lt;
> current()/time])"/>
>          <xsl:copy-of select="."/>
>        </xsl:for-each>
>      </xsl:for-each>
>   </xsl:template>
> </xsl:stylesheet>
>
> When applied on this source.xml (you never provided your original xml, so
I
> use my own here):
>
> <records>
>  <record name="abc" date="20011130">
>    <time>12</time>
>  </record>
>  <record name="abc" date="19991130">
>    <time>12</time>
>  </record>
>  <record name="abc" date="20011130">
>    <time>18</time>
>  </record>
>  <record name="abc" date="20011130">
>    <time>11</time>
>  </record>
>  <record name="abc" date="19991130">
>    <time>11</time>
>  </record>
>  <record name="abc" date="20011130">
>    <time>22</time>
>  </record>
>  <record name="abc" date="20011130">
>    <time>15</time>
>  </record>
> </records>
>
> The wanted result is produced:
>
>
> 1<record name="abc" date="19991130">
>    <time>11</time>
>  </record>
> 2<record name="abc" date="20011130">
>    <time>11</time>
>  </record>
> 3<record name="abc" date="19991130">
>    <time>12</time>
>  </record>
> 4<record name="abc" date="20011130">
>    <time>12</time>
>  </record>
> 5<record name="abc" date="20011130">
>    <time>15</time>
>  </record>
> 6<record name="abc" date="20011130">
>    <time>18</time>
>  </record>
> 7<record name="abc" date="20011130">
>    <time>22</time>
>  </record>
>
>
> =====
> Cheers,
>
> Dimitre Novatchev.
> http://fxsl.sourceforge.net/ -- the home of FXSL
>
>
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.