By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,658 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,658 IT Pros & Developers. It's quick & easy.

XSL sorting problem

P: n/a
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.

Jul 20 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Anyone?

Jul 20 '05 #2

P: n/a

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
Jul 20 '05 #3

P: n/a
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).

Jul 20 '05 #4

P: n/a
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

Jul 20 '05 #5

P: n/a
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 ;-)

Jul 20 '05 #6

P: n/a
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?

Jul 20 '05 #7

P: n/a
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.

Jul 20 '05 #8

P: n/a
re***@hotmail.com writes:
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
Jul 20 '05 #9

P: n/a
Would it be possible if you can show me a simple example as how to do
it.

Jul 20 '05 #10

P: n/a
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.

Jul 20 '05 #11

P: n/a
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.

ja******@gmx.de wrote:
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.


Jul 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.