> 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() >= $start_id and position()
<= $end_id)">
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) >=
($date - $new_days)]"/>
<xsl:apply-templates select="current()[number( concat(
substring(Date,1,4),substring(Date,6,2),substring( Date,9,2) ) ) <
($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&id=</xsl:text>
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:value-of select="Title"/>
</a>
<xsl:if test="$entry_date >= ($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