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

Sorting

P: n/a
Let's suppose I have this sample document:

<root>
<entry id="1"
<date>2003-08-03</date>
<param_1>5</param_1>
<param_2>10</param_2>
</entry>
<entry id="2">
...
</entry>
</root>

Given some template to apply to this data, firstly I want to sort these
entries according to content of "date" element. Secondly, I would like to
split the result tree into two parts - the documents which are newer than 30
days and the rest, and following that I need to sort these groups according
to value of param_2 and param_1 (in the given order). The code I have tried
looks like this:

<xsl:apply-templates select="entry[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &lt;
($date - $new_days)]">
<xsl:sort select="param_2" order="descending"/>
<xsl:sort select="param_1" order="descending"/>
<xsl:sort select="date" order="descending"/>
</xsl:apply-templates>

But there's some caveat: the data gets sorted by param_2 and if there are
two entries with equal value, they are not sorted further by param_1. The
same applies the level below - if there are two entries with equal values of
param_1, they don't get sorted by date. What's wrong here? How can I make
this work the way I want it to?

I use PHP 4.3.2 with DOM XML.
--

__ .: Radoslaw Loboda :. | Cool stuff !!!
(oo) rl*****@bojko.krakow.pl | --------------------
/ \/ \ | www.tigerhomes.org
`V__V' | www.suncam.tv

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


P: n/a
Can you provide a complete (but the smallest possible) example --
source.xml, your transformation, the result and how it doesn't meet
your expectations.
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"p0wer" <p0wer@bojko_krakow_pl> wrote in message news:<3f********@news.vogel.pl>...
Let's suppose I have this sample document:

<root>
<entry id="1"
<date>2003-08-03</date>
<param_1>5</param_1>
<param_2>10</param_2>
</entry>
<entry id="2">
...
</entry>
</root>

Given some template to apply to this data, firstly I want to sort these
entries according to content of "date" element. Secondly, I would like to
split the result tree into two parts - the documents which are newer than 30
days and the rest, and following that I need to sort these groups according
to value of param_2 and param_1 (in the given order). The code I have tried
looks like this:

<xsl:apply-templates select="entry[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &lt;
($date - $new_days)]">
<xsl:sort select="param_2" order="descending"/>
<xsl:sort select="param_1" order="descending"/>
<xsl:sort select="date" order="descending"/>
</xsl:apply-templates>

But there's some caveat: the data gets sorted by param_2 and if there are
two entries with equal value, they are not sorted further by param_1. The
same applies the level below - if there are two entries with equal values of
param_1, they don't get sorted by date. What's wrong here? How can I make
this work the way I want it to?

I use PHP 4.3.2 with DOM XML.

Jul 20 '05 #2

P: n/a
> Can you provide a complete (but the smallest possible) example --
source.xml, your transformation, the result and how it doesn't meet
your expectations.


It is a basic link list built with XML:

<?xml version="1.0" encoding="UTF-8"?>
<LinkList>
<Entries>
<Entry id="1">
<SomeTags/>
<Date>2003-08-01</Date>
<SomeTags/>
<LinkData>
<Hits count="10"/>
<Rates>
<rate>6</rate>
<rate>4</rate>
</Rates>
<SomeTags/>
</LinkData>
</Entry>
<Entry id="2">
<SomeTags/>
<Date>2003-09-10</Date>
<SomeTags/>
<LinkData>
<Hits count="5"/>
<Rates>
<rate>9</rate>
<rate>1</rate>
</Rates>
<SomeTags/>
</LinkData>
</Entry>
</Entries>
<SomeTags/>
</LinkList>

What I want is to simply generate a list, where:
- newest entries (let's say not older than 30 days) are placed on the top of
the list
- newest entries and the rest are sorted separately
- sorting order of each group is Hits-Rate (which is a simple average of all
rates)
- each output page displays a defined number of links (passed as param by
PHP)

Seems like I can't get it the recursive way because XSLT does not allow to
reassign variable values. I have managed though to make it sort the entries
in the top-level approach, that means I have first sorted them according to
Hits, then Rate and Date coming last. But trying to output all the new
entries first, and then the rest, still maintaining the order and limiting
the number of links per page seems too hard for me.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso8859-1" indent="yes"
standalone="yes"/>

<!-- Variables used in document -->
<xsl:param name="style_css"/>
<xsl:param name="start_id">0</xsl:param>
<xsl:param name="end_id">0</xsl:param>
<xsl:param name="date"/>
<xsl:param name="new_days">30</xsl:param>

<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="{$style_css}"/>
</head>
<body scroll="auto">
<table width="100%" height="100%" border="0" cellpadding="3"
cellspacing="0">
<tbody>
<tr>
<td height="100%" align="center" valign="top">

<xsl:call-template name="date_check"/>

</td>
</tr>
</tbody>
</table>
</body>
</html>
</xsl:template>

<xsl:template name="date_check">
<xsl:param name="new">0</xsl:param>
<xsl:for-each select="/LinkList/Entries/Entry">
<xsl:sort select="LinkData/Hits/@count" order="descending"/>
<xsl:sort select="sum(LinkData/Rates/rate) div count(LinkData/Rates/rate)"
order="descending"/>
<xsl:sort select="Date" order="descending"/>
<xsl:value-of select="position()"/>
<xsl:if test="$end_id = 0 or (position() &gt;= $start_id and position()
&lt;= $end_id)">
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &gt;=
($date - $new_days)]"/>
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &lt;
($date - $new_days)]"/>
</xsl:if>
</xsl:for-each>
</xsl:template>

<xsl:template match="Entry">
<xsl:variable name="entry_date">
<xsl:value-of select="number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) )"/>
</xsl:variable>

<!-- This is just simple display - trimmed down -->
<table width="100%" border="0" cellpadding="0" cellspacing="0"
style="border:none">
<tbody>
<tr>
<td align="left" valign="middle">
<a>
<xsl:attribute name="href">
<xsl:text>?action=view&amp;id=</xsl:text>
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:value-of select="Title"/>
</a>
<xsl:if test="$entry_date &gt;= ($date - 30)">
 <span class="red">NEW!</span>
</xsl:if>
</td
<td align="center" width="80" height="40">
<div class="red">
<xsl:value-of select="LinkData/Hits/@count"/>
</div>
<span class="small">
<br/>(<xsl:value-of select="Date"/>)
</span>
</td>
<td align="center" width="40">
<span class="red">

<xsl:value-of select="format-number( ( sum(LinkData/Rates/rate) ) div
( count(LinkData/Rates/rate) ),'0.0')"/>

</span>
</td>
</tr>
</tbody>
</table>
</xsl:template>

Let's say we have this data:
- id=1 & id=2 are old entries
- id=3 & id=4 are new entries
- id=2 has hits=2
- id=3 has rate=2
- 2 links per page
After sorting the order is 2-3-4-1, which is correct. But right now I get it
only this way. What I need is 3-4-2-1... The two entries for apply-templates
were used for some conditional choices, but it didn't work out exactly what
was expected. The selects are working, but now I need to put them somewhere
else to group the nodes.
--

__ .: Radoslaw Loboda :. | Cool stuff !!!
(oo) rl*****@bojko.krakow.pl | --------------------
/ \/ \ | www.tigerhomes.org
`V__V' | www.suncam.tv

Jul 20 '05 #3

P: n/a
Your problem is that you are combining several problems in one and are
trying to solve them at once.

This can be solved by addressing each problem individually and then
combining the solutions. Probably a multi-pass solution will be one of the
easiest possible.

However, the problem in your code is not at all the sorting.

It seems to me that your date arithmetic is not OK.

In particular:
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &gt;=
($date - $new_days)]"/>
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &lt;
($date - $new_days)]"/>

the above calculation of "new" and "old" links will not work correctly in
most of the cases.
So, I would recommend that you try to split the task into sub-problems,
which are more realistic and manageable and then you have greater chances to
succeed.

You may find a dates/time library of templates useful. One such library is
the datetime_lib.xsl library, which comes with the XSelerator and can be
downloaded with the trial version of the product.
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"p0wer" <p0wer@bojko_krakow_pl> wrote in message
news:3f******@news.vogel.pl...
Can you provide a complete (but the smallest possible) example --
source.xml, your transformation, the result and how it doesn't meet
your expectations.


It is a basic link list built with XML:

<?xml version="1.0" encoding="UTF-8"?>
<LinkList>
<Entries>
<Entry id="1">
<SomeTags/>
<Date>2003-08-01</Date>
<SomeTags/>
<LinkData>
<Hits count="10"/>
<Rates>
<rate>6</rate>
<rate>4</rate>
</Rates>
<SomeTags/>
</LinkData>
</Entry>
<Entry id="2">
<SomeTags/>
<Date>2003-09-10</Date>
<SomeTags/>
<LinkData>
<Hits count="5"/>
<Rates>
<rate>9</rate>
<rate>1</rate>
</Rates>
<SomeTags/>
</LinkData>
</Entry>
</Entries>
<SomeTags/>
</LinkList>

What I want is to simply generate a list, where:
- newest entries (let's say not older than 30 days) are placed on the top

of the list
- newest entries and the rest are sorted separately
- sorting order of each group is Hits-Rate (which is a simple average of all rates)
- each output page displays a defined number of links (passed as param by
PHP)

Seems like I can't get it the recursive way because XSLT does not allow to
reassign variable values. I have managed though to make it sort the entries in the top-level approach, that means I have first sorted them according to Hits, then Rate and Date coming last. But trying to output all the new
entries first, and then the rest, still maintaining the order and limiting
the number of links per page seems too hard for me.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso8859-1" indent="yes"
standalone="yes"/>

<!-- Variables used in document -->
<xsl:param name="style_css"/>
<xsl:param name="start_id">0</xsl:param>
<xsl:param name="end_id">0</xsl:param>
<xsl:param name="date"/>
<xsl:param name="new_days">30</xsl:param>

<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="{$style_css}"/>
</head>
<body scroll="auto">
<table width="100%" height="100%" border="0" cellpadding="3"
cellspacing="0">
<tbody>
<tr>
<td height="100%" align="center" valign="top">

<xsl:call-template name="date_check"/>

</td>
</tr>
</tbody>
</table>
</body>
</html>
</xsl:template>

<xsl:template name="date_check">
<xsl:param name="new">0</xsl:param>
<xsl:for-each select="/LinkList/Entries/Entry">
<xsl:sort select="LinkData/Hits/@count" order="descending"/>
<xsl:sort select="sum(LinkData/Rates/rate) div count(LinkData/Rates/rate)" order="descending"/>
<xsl:sort select="Date" order="descending"/>
<xsl:value-of select="position()"/>
<xsl:if test="$end_id = 0 or (position() &gt;= $start_id and position()
&lt;= $end_id)">
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &gt;=
($date - $new_days)]"/>
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &lt;
($date - $new_days)]"/>
</xsl:if>
</xsl:for-each>
</xsl:template>

<xsl:template match="Entry">
<xsl:variable name="entry_date">
<xsl:value-of select="number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) )"/>
</xsl:variable>

<!-- This is just simple display - trimmed down -->
<table width="100%" border="0" cellpadding="0" cellspacing="0"
style="border:none">
<tbody>
<tr>
<td align="left" valign="middle">
<a>
<xsl:attribute name="href">
<xsl:text>?action=view&amp;id=</xsl:text>
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:value-of select="Title"/>
</a>
<xsl:if test="$entry_date &gt;= ($date - 30)">
 <span class="red">NEW!</span>
</xsl:if>
</td
<td align="center" width="80" height="40">
<div class="red">
<xsl:value-of select="LinkData/Hits/@count"/>
</div>
<span class="small">
<br/>(<xsl:value-of select="Date"/>)
</span>
</td>
<td align="center" width="40">
<span class="red">

<xsl:value-of select="format-number( ( sum(LinkData/Rates/rate) ) div
( count(LinkData/Rates/rate) ),'0.0')"/>

</span>
</td>
</tr>
</tbody>
</table>
</xsl:template>

Let's say we have this data:
- id=1 & id=2 are old entries
- id=3 & id=4 are new entries
- id=2 has hits=2
- id=3 has rate=2
- 2 links per page
After sorting the order is 2-3-4-1, which is correct. But right now I get it only this way. What I need is 3-4-2-1... The two entries for apply-templates were used for some conditional choices, but it didn't work out exactly what was expected. The selects are working, but now I need to put them somewhere else to group the nodes.
--

__ .: Radoslaw Loboda :. | Cool stuff !!!
(oo) rl*****@bojko.krakow.pl | --------------------
/ \/ \ | www.tigerhomes.org
`V__V' | www.suncam.tv

Jul 20 '05 #4

P: n/a
Użytkownik "Dimitre Novatchev" <dn********@yahoo.com> napisał w wiadomości
news:bj************@ID-152440.news.uni-berlin.de...
Your problem is that you are combining several problems in one and are
trying to solve them at once.

This can be solved by addressing each problem individually and then
combining the solutions. Probably a multi-pass solution will be one of the
easiest possible.
And trying to do multi-pass in one XSLT document? Interesting, have some
good/advanced examples?
However, the problem in your code is not at all the sorting.

It seems to me that your date arithmetic is not OK.

In particular:
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &gt;=
($date - $new_days)]"/>
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &lt;
($date - $new_days)]"/>

the above calculation of "new" and "old" links will not work correctly in
most of the cases.


I said it works OK. I know that when I want one month back I have to pass
100 as new_days. I suppose you thought that a bit better would be supplying
a timestamp and playing with it. Anyway, the select staatements could be
used in some other place, but still trying to make up where will they fit...
So, I would recommend that you try to split the task into sub-problems,
which are more realistic and manageable and then you have greater chances to succeed.
The version which I have supplied is indeed a mix of all the separate stuff.
You may find a dates/time library of templates useful. One such library is
the datetime_lib.xsl library, which comes with the XSelerator and can be
downloaded with the trial version of the product.


I might try installing some extensions, but what if I move it to the server
which does not support them? Admins are unlikely to do it for one user and
this site is made for other person. Instead, I am trying to get it work
without any extensions...
--

__ .: Radoslaw Loboda :. | Cool stuff !!!
(oo) rl*****@bojko.krakow.pl | --------------------
/ \/ \ | www.tigerhomes.org
`V__V' | www.suncam.tv

Jul 20 '05 #5

P: n/a
> > It seems to me that your date arithmetic is not OK.

In particular:
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &gt;=
($date - $new_days)]"/>
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) &lt;
($date - $new_days)]"/>

the above calculation of "new" and "old" links will not work correctly in most of the cases.


I said it works OK. I know that when I want one month back I have to pass
100 as new_days. I suppose you thought that a bit better would be

supplying a timestamp and playing with it. Anyway, the select staatements could be
used in some other place, but still trying to make up where will they fit...

But one month can be 28, 29, 30 or 31 days...

What about going from December to January?
You may find a dates/time library of templates useful. One such library is the datetime_lib.xsl library, which comes with the XSelerator and can be
downloaded with the trial version of the product.


I might try installing some extensions, but what if I move it to the

server which does not support them? Admins are unlikely to do it for one user and
this site is made for other person. Instead, I am trying to get it work
without any extensions...


What I recommended is not extensions -- it is pure XSLT 1.0 and you don't
have to install anything -- just copy this .xsl file in your folder.

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Jul 20 '05 #6

P: n/a
Użytkownik "Dimitre Novatchev" <dn********@yahoo.com> napisał w wiadomości
news:bj************@ID-152440.news.uni-berlin.de...
But one month can be 28, 29, 30 or 31 days...

What about going from December to January?
The year also changes :) Didn't notice? Stripping slashes gives a number
like 20030911.
What I recommended is not extensions -- it is pure XSLT 1.0 and you don't
have to install anything -- just copy this .xsl file in your folder.


I will give it a try tommorow, although current version of PHP DOMXML
(4.3.2rc3) is said to be having problems with including XSLT's inside other
XSLT's ("The method can not perform an XSLT transformation, where an XSL
uses imported XSL")
--

__ .: Radoslaw Loboda :. | Cool stuff !!!
(oo) rl*****@bojko.krakow.pl | --------------------
/ \/ \ | www.tigerhomes.org
`V__V' | www.suncam.tv

Jul 20 '05 #7

P: n/a

"p0wer" <p0wer@bojko_krakow_pl> wrote in message
news:3f********@news.vogel.pl...
Użytkownik "Dimitre Novatchev" <dn********@yahoo.com> napisał w wiadomości
news:bj************@ID-152440.news.uni-berlin.de...
But one month can be 28, 29, 30 or 31 days...

What about going from December to January?


The year also changes :) Didn't notice? Stripping slashes gives a number
like 20030911.


Yes, I know this. But how do you add 15 days to 20021217 ?

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
Jul 20 '05 #8

P: n/a
Użytkownik "Dimitre Novatchev" <dn********@yahoo.com> napisał w wiadomości
news:bj************@ID-152440.news.uni-berlin.de...
"p0wer" <p0wer@bojko_krakow_pl> wrote in message
The year also changes :) Didn't notice? Stripping slashes gives a number
like 20030911.


Yes, I know this. But how do you add 15 days to 20021217 ?


The functions you are talking about are quite nice. In fact I was thinking
exactly the same - julian date can be also calculated as integer part of
(timestamp/86400) (I think so...). I wonder how could I use these functions
for every date in each entry... Define a variable inside foreach? Please
help me with this algorithm...
--

__ .: Radoslaw Loboda :. | Cool stuff !!!
(oo) rl*****@bojko.krakow.pl | --------------------
/ \/ \ | www.tigerhomes.org
`V__V' | www.suncam.tv

Jul 20 '05 #9

P: n/a

"p0wer" <p0wer@bojko_krakow_pl> wrote in message
news:3f********@news.vogel.pl...
Użytkownik "Dimitre Novatchev" <dn********@yahoo.com> napisał w wiadomości
news:bj************@ID-152440.news.uni-berlin.de...
"p0wer" <p0wer@bojko_krakow_pl> wrote in message
The year also changes :) Didn't notice? Stripping slashes gives a number like 20030911.
Yes, I know this. But how do you add 15 days to 20021217 ?


The functions you are talking about are quite nice. In fact I was thinking
exactly the same - julian date can be also calculated as integer part of
(timestamp/86400) (I think so...). I wonder how could I use these

functions for every date in each entry... Define a variable inside foreach? Please
help me with this algorithm...

See the code of my Calendar Application, available at:

http://www.topxml.com/code/default.a...20020711152545

This XSLT app displays the quarters, months, weeks, calendar and weekdays of
the current year. Today's date and any date from the Holidays.xml file are
highlighted. Placing the cursor over a specific holiday produces a tip,
describing this event. You can customize it by editing the Holidays.xml file
and specifying your own holidays/events data there.
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.