[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Sorting Two Dimensional Table


Subject: RE: [xsl] Sorting Two Dimensional Table
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Mon, 15 Oct 2007 11:43:20 -0500

Pierre-Luc,

What I was trying to get across is that it seems you want the actual row-sort
to happen at the <entries> level. (I might've flubbed that in the last version
I wrote - I had a longer one written and then chopped it down.) As far as your
sort key names, you'll have to determine how many of them you'll have, and
keep that as a static number. From there, try this:

<xsl:variable name="key1-name">
  <xsl:for-each select="//titles/key">
    <xsl:sort/>
     <xsl:if test="position() = 1">
       <xsl:value-of select="."/>
     </xsl:if>
  </xsl:for-each>
</xsl:variable>
<xsl:variable name="key2-name">
  <xsl:for-each select="//titles/key">
    <xsl:sort/>
     <xsl:if test="position() = 2">
       <xsl:value-of select="."/>
     </xsl:if>
  </xsl:for-each>
</xsl:variable>
<!-- ... ad nauseum ... -->


<!-- this is the important one -->
<xsl:template match="entries">
  <xsl:apply-templates select="entry">
    <xsl:sort select="key[@*[name() = $key1-name]]/@value"/>
    <xsl:sort select="key[@*[name() = $key2-name]]/@value"/>
    <!-- ... similar ad nauseum list here ... -->
    <xsl:sort select="value"/>
  </xsl:apply-templates>
</xsl:template>


<xsl:template match="titles">
  <th>
    <xsl:value-of select="key[. = $key1-name]"/>
  </th>
  <th>
    <xsl:value-of select="key[. = $key2-name]"/>
  </th>
  <!-- ... similar ad nauseum list here ... -->
  <th>
    <xsl:value-of select="value"/>
  </th>
</xsl:template>

<xsl:template match="entry">
  <td>
    <xsl:value-of select="key[@*[name() = $key1-name]]/@value"/>
  </td>
  <td>
    <xsl:value-of select="key[@*[name() = $key2-name]]/@value"/>
  </td>
  <!-- ... similar ad nauseum list here ... -->
  <td>
    <xsl:value-of select="value"/>
  </td>
</xsl:template>


Oh, and this is XSLT 1.0. Not sure what a simpler 2.0 one would look like, but
others here would.

~ Scott


-----Original Message-----
From: Pierre-Luc Bertrand [mailto:Pierre-Luc.Bertrand@xxxxxxxxxx]
Sent: Monday, October 15, 2007 11:23 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Sorting Two Dimensional Table

Hi,

I don't think that this solution is what I'm looking for after trying it a
bit. It seems here that you are sorting on key name and then on key value but
this is not what I want to do.

First I'm sorting my key name for presenting as titles in my table:

a b c value

Then I need to sort the data accordingly. Here the data has keys and value
pair. The keys are presented as follow: the name of the key is to put it under
the right column and the value of it is to show that value in the column.

So something like this:
<titles>
	<key>z</key>
	<key>a</key>
	<value>valueX</value>
</titles>

Would result as table title as follow:

a z valueX

Then entries as follow:

<entries>
  <entry>
    <key name="a" value="b" />
    <key name="z" value="c" />
    <value>myValue1</value>
  </entry>
  <entry>
    <key name="a" value="b" />
    <key name="z" value="d" />
    <value>5</value>
  </entry>
  <entry>
    <key name="a" value="a" />
    <key name="z" value="c" />
    <value>myValue3</value>
  </entry>
  <entry>
    <key name="a" value="b" />
    <key name="z" value="d" />
    <value>4</value>
  </entry>
</entries>

Would be presented as follow:

a z valueX
------------
a c myValue3 (because 'a' goes before 'b' in column 'a')

b c myValue1 (because 'b' goes after 'a' in column 'a' but 'c' goes before 'd'
in column 'z')

b d 4        (because 'b' goes after 'a' in column 'a' and 'd' goes after 'c'
in column 'z' and '4' goes before '5' in column 'valueX')

b d 5        (because 'b' goes after 'a' in column 'a' and 'd' goes after 'c'
in column 'z' but '5' goes after '4' in column 'valueX')

So what is required is a sort on the title name on a single row and then show
the value and a global sort on the data based first on column titled a, then
on z (because we sort keys first) and then on column x (because it is the
value).

In pseudo code, it would look like this:

for each titles
  keyNameSorted := sort key alphabetically
  print keys
  print value
end for each titles

for each i in keyNameSorted
  sort the data based on keyNameSorted[i]'s value
end for each I in keyNameSorted

for each entry
  for each i in keyNameSorted
    print value associated to key named keyNameSorted[i]
  end for each I in keyNameSorted

  print value
end for each entry


Thanks a lot for your time

Pierre.

-----Original Message-----
From: Scott Trenda [mailto:Scott.Trenda@xxxxxxxx]
Sent: Monday, October 15, 2007 10:56 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Sorting Two Dimensional Table

Pierre,

Remember that you can use multiple <xsl:sort/> statements together. Try
something like this:

<xsl:template match="entry">
  <xsl:apply-templates select="key">
    <xsl:sort select="@name"/>
    <xsl:sort select="@value"/>
  </xsl:apply-templates>
  <xsl:apply-templates select="value"/>
</xsl:template>

~ Scott


-----Original Message-----
From: Pierre-Luc Bertrand [mailto:Pierre-Luc.Bertrand@xxxxxxxxxx]
Sent: Monday, October 15, 2007 9:41 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Sorting Two Dimensional Table

Thanks Scott,

Hotmail is great !!
Again,
 
 
Hi,
 
I'm desperately trying to sort a two dimension table.
 
I have keys and values. I want to be able to sort by keys and then by value.
This is not a problem so far. The problem comes when I try to sort the keys
that I want to be sorted as well.
 
Example:
x y value
a a a
a b a
a a b
a b b
a a c
b b a
b a b
b b b
b a d

which is presented as follow:

<table>
 <titles>
  <key>a</key>
  <key>b</key>
  <value>halo_name</value>
 </titles>
 
 <entries>
  <entry>
   <key name="b" value="a" />
   <key name="a" value="b" />
   <value>d</value>
  </entry>
  <entry>
   <key name="a" value="a" />
   <key name="b" value="a" />
   <value>c</value>
  </entry>
  <entry>
   <key name="a" value="a" />
   <key name="b" value="a" />
   <value>b</value>
  </entry>
  <entry>
   <key name="a" value="a" />
   <key name="b" value="b" />
   <value>b</value>
  </entry>
  <entry>
   <key name="a" value="b" />
   <key name="b" value="a" />
   <value>b</value>
  </entry>
  <entry>
   <key name="a" value="a" />
   <key name="b" value="a" />
   <value>a</value>
  </entry>
  <entry>
   <key name="a" value="b" />
   <key name="b" value="b" />
   <value>a</value>
  </entry>
  <entry>
   <key name="a" value="a" />
   <key name="b" value="b" />
   <value>a</value>
  </entry>
  <entry>
   <key name="a" value="b" />
   <key name="b" value="b" />
   <value>b</value>
  </entry>
 </entries>
</table>
 
Th problem is that I don't know the keys beforhand neither how many of them I
have. So to show the title of my table, I use something like that:
 
<xsl:for-each select="titles/key">
<xsl:sort select="." />
<th bgcolor="#9acd32" align="left"><xsl:value-of select="." /></th>
</xsl:for-each>
<th bgcolor="cornflowerblue" align="left"><xsl:value-of select="titles/value"
/></th>
 
 
So that I know that my key titles are always sorted and then I put the data in
the same fashion
 
<xsl:for-each select="entries/entry">
<tr>
<xsl:for-each select="key">
<xsl:sort select="@name" />
<td align="left"><xsl:value-of select="@value" /></td>
</xsl:for-each>
<td align="left"><xsl:value-of select="value" /></td>
</tr>
</xsl:for-each>
 
 
So I'd like to sort my keys and then sort the data accordingly so that
visually the first column is sorted first and the second column second...
 
Help !
 
Thank you very much.
 
PL


> Date: Mon, 15 Oct 2007 09:15:58 -0500
> From: Scott.Trenda@xxxxxxxx
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] Sorting Two Dimensional Table
>
> Hey PL,
>
> Try sending that again? It looks like your examples got nerfed when you
> sent it.
>
>
> ~ Scott
>
>
> -----Original Message-----
> From: P L [mailto:happytchoum@xxxxxxxxxxx]
> Sent: Monday, October 15, 2007 9:10 AM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Sorting Two Dimensional Table
>
>
> Hi,
>
> I'm desperately trying to sort a two dimension table.
>
> I have keys and values. I want to be able to sort by keys and then by
> value. This is not a problem so far. The problem comes when I try to
> sort the keys that I want to be sorted as well.
>
> Example:
>
> x y value
>
> a a a
> a b a
> a a b
> a b b
> a a c
> b b a
> b a b
> b b b
> b a d
>
> which is presented as follow:
>
>
>
> a
> b
> halo_name
>
>
>
>
>
>
> d
>
>
>
>
> c
>
>
>
>
> b
>
>
>
>
> b
>
>
>
>
> b
>
>
>
>
> a
>
>
>
>
> a
>
>
>
>
> a
>
>
>
>
> b
>
>
>
>
> Th problem is that I don't know the keys beforhand neither how many of
> them I have. So to show the title of my table, I use something like
> that:
>
>
>
>
>
>
>
>
>
>
>
>
>
> So that I know that my key titles are always sorted and then I put the
> data in the same fashion
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> So I'd like to sort my keys and then sort the data accordingly so that
> visually the first column is sorted first and the second column
> second...
>
> Help !
>
> Thank you very much.
>
> PL
>
> _________________________________________________________________
> Envoie un sourire, fais rire, amuse-toi! Employez-le maintenant!
> http://www.emoticonesgratuites.ca/?icid=EMFRCA120
>

________________________________________
Envoie un sourire, fais rire, amuse-toi! Employez-le maintenant!


Current Thread
Keywords