XSL sorting problem

R

ree32

I have a problem with XSL sorting. The problem is that I need to create
a 2 column table so I am using this.

<xsl:apply-templates select="//Photo[position()mod 2=1]">
</xsl:apply-templates>

With template
<xsl:template match="//Photo">
.....
</xsl:template>

The problem arises when I want to sort the Photos by date (field name
Taken)

I tried this
<xsl:apply-templates select="//Photo[position()mod 2=1]">
<xsl:sort select="Taken"/>
</xsl:apply-templates>

With template
<xsl:template match="//Photo">
.....
</xsl:template>

but as you could see it is sorting the first columns of photos by date
but the second column is not sorted, as the second column is produced
using :
following-sibling::*/XXXXX


When I tried this:
<xsl:apply-templates select="//Photo">
<xsl:sort select="Taken"/>
</xsl:apply-templates>

With template
<xsl:template match="//Photo[position()mod 2=1]">
.....
</xsl:template>

The table is fine but its printing out all of the data from some Photo
nodes at the top of the page.
ie.
181001047_20050627_001.jpgtmb_001047_20050627_001.jpga27/06/2005
12:00:00
AM22001047_20050628_002.jpgtmb_001047_20050628_002.jpg2planks28/06/2005
12:00:00 AM44001047_20050628_004.jpgtmb_001047_20050628_004.jpg4
28/06/2005 12:00:00
AM66001047_20050628_006.jpgtmb_001047_20050628_006.jpg28/06/2005
12:00:00 AM

Can someone tell me how to solve this problem.
 
D

David Carlisle

do it in two stages, first sort then group.
Either as two separate sheets, or using a node-set() extension so you
can do two passes in a single stylesheet (almost every xslt1 engine has
this extension with mozilla's transformiix being the notable exception)
or use xslt2 draft implementation such as saxon8 where node-set() isn't
needed.

David
 
J

jan00000

Although I do not understand completely what you want, it seems to me
like you might want to use some variables. Store in one variable var1
all Photo elements with position() mod2 =1, and in another var2 all
Photo elements with mod 2=0. Then you can sort both of these
node-lists.

If this did not help, try and give me a more concrete example (e.g.,
the original xml, and how you'd like it to look after the
transformation).
 
R

ree32

xml file
<Photolist>
<Photo>
<ID>1</ID>
<Instance>1</Instance>
<Filename>001046_20050630_001.jpg</Filename>
<Thumbnail>tmb_001046_20050630_001.jpg</Thumbnail>
<Caption>
</Caption>
<Description>
</Description>
<Taken>2005-06-30T00:00:00.0000000+10:00</Taken>
</Photo>
<Photo>
<ID>2</ID>
<Instance>2</Instance>
<Filename>001046_20050630_002.jpg</Filename>
<Thumbnail>tmb_001046_20050630_002.jpg</Thumbnail>
<Caption>
</Caption>
<Description>
</Description>
<Taken>2005-06-30T00:00:00.0000000+10:00</Taken>
</Photo>
.....

I want the transformation to be a HTML file that forms a thumnail
gallery, a table with 2 columns. The thumbnails should be order from
the latest "Taken" at the top row.
Ie.
<html>..

<table>
<tr><td>thumbnail1</td><td>thumbnail2</td></tr>
<tr><td>thumbnail3</td><td>thumbnail4</td></tr>
<tr><td>thumbnail5</td><td>thumbnail6</td></tr>
</table>


Thanks
 
J

jan00000

OK, try the following:

1.) sort all thumbnails by date and store them in a variable (e.g.
"sorted")
2.) select the first, third etc. as you did above by using mod 2 = 1,
but from $sorted instead of the original document, and store the
result in another variable ("uneven")
3.) select the second, fouth etc. thumbnail by using mod 2 = 0 on
$sorted, store in ("even")
4.) Use a for-each on "uneven" and construct you table rows.
5.) Watch out for the last row ... it might not contain an element in
"$even"

There might be a more elegant way, but I could not come up with one.
But this should work ;-)
 
R

ree32

I am having a problem with no.4. as I don't how to place even & odd
photos side by side in a table row. As its easy to create a row of just
evenphotos but how do I get the odd into it?
 
R

ree32

I am also having problems with getting nodes sorted into a variable.

I tried this
<xsl:for-each select="//Photolist">
<xsl:sort select="Taken"/>
<xsl:variable name="sorted" select="//Photo" />
</xsl:fore-each>

But it doesn't sort it.
 
D

David Carlisle

I am also having problems with getting nodes sorted into a variable.

I tried this
<xsl:for-each select="//Photolist">
<xsl:sort select="Taken"/>
<xsl:variable name="sorted" select="//Photo" />
</xsl:for-each>

But it doesn't sort it.

In the above the variable would go out of scope after the for-each so it
could not be used, but in general Variables hold node sets, and sets 9as
opposed to lists or sequences) are always unordered. You can not hold an
ordering in XSLt1 you can only affect the order in which the processing
occirs using xsl:sort.

David
 
J

jan00000

Hi,

I'm sorry, I couldn't figure it out myself. Seems that it doesn't work
the way I thought it should.

Unfortunately, you'll need to find another way I do not yet see ...
Sorry I couldn't help solve your problem.
 
R

ree32

Its amazing my first attempt at XML/XSL, and I can stump it. I think
its better to stick to a "real" language when it comes to real world
sorting of data as XML/XSL is only viable for very simple data
processing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top