How to solve this?

Hi,

I encounter a problem with XSLT and xml.
My xml has got the following format:

<PressReleases>
<PressRelease year="2003">
.......
</PressRelease>
<PressRelease date="2002">
.......
</PressRelease>
<PressRelease date="2001">
.......
</PressRelease>
<PressRelease date="2000">
.......
</PressRelease>
</PressReleases>

Inside my XSLT, I will sort the <PressRelease> elements in terms of year in descending order and then have a input parameter "noPressRelease" to display the N number of recent press releases (must be the latest to the earlier). There is also a fromYear parameter and toYear parameter that determines the range of press releases to display.

The following code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl: output method="html"/>
<xsl: param name="noPressRelease">2</xsl: param>
<xsl: param name="fromYear">2002</xsl: param>
<xsl: param name="toYear">2001</xsl: param>
<!-- document root node -->
<xsl:template match="PressReleases">
<html>
<head>
<title>Press Releases</title>
<link rel="stylesheet" type="text/css" href="pressRelease.css"/>
</head>
<body>
<table cellpadding="5" cellspacing="0" border="0">
<!--sort PressRelease according to descending dated attribute-->
<xsl:apply-templates select="PressRelease">
<xsl:sort
select="@year"
order="descending">
</xsl:sort>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>

<!--The problem is here.... noted with :P-->
<!-- Deal with each PressRelease Element -->
<xsl:template match="PressRelease">
<xsl:if test="$fromYear <= @year and $toYear >= year"> :P
<xsl:if test="position()<=$noPressRelease"> :P
<tr>
<td>
<xsl:value-of select="position"/>
<!--display pressRelease year-->
<!--display pressRelease title-->
</td>
</tr>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Suppose I change my fromYear parameter to 2003 and toYear paramter to 2000, and my noPressRelease paramter to 4 (no of press releases to be displayed). When I transformation my xml using my XSLT, it will sort the four press releases in descending order and return the following:

The number of the left is value of position()

1) 2003 Title....
2) 2002 Title....
3) 2001 Title....
4) 2000 Title....

This is ok. However the position() is already fixed for each press Release.

If I want to select fromYear parameter as 2002 and toYear as 2001, and noPressRelease parameter as 4, then the following will return:

2) 2002 Title....
3) 2001 Title....

however, if I change the noPressRelease parameter to 1 and the fromYear as 2002 and toYear as 2001, which implies I only want to display only 1 press release (the latest one only): the result should be 2)2002 Title...

However, the result is none!

I know the reason is because the position() is 2 and 3 and that is filtered by the fromDate and toDate parameters in the first <xsl:if> statement. But when it comes to the second <xsl:if> statement, it does not go through as position 2 and 3 is greater than the noPressRelease parameter which is 1. Therefore it cannot display the second press Release at position 2.

But I need to display that...
This implies if the noPressRelease paramter is 2 instead, then I will need to display the press Releases at position() 2 and 3.

Can anyone advice? Is there a way to resolve this problem?
[4432 byte] By [chenlongtai] at [2007-11-18 17:01:32]
# 1 Re: How to solve this?
There are several problems in your code, like the fact that you use @year in the stylesheet, but set a date attribute in the xml file.

Since you seem to have had this working, I can only assume that you intentionally messed up the stylesheet/xml file, just to annoy anyone who might try to help you.

Let me give you some advice, if you want help with your code, post the code that behaves like you say it does. I really really hate, having to wade through a dusin other problems that you must have introduced on purpose.
khp at 2007-11-10 3:28:23 >