XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Xin HeSubject: Display top n items from categories in certain sequence
Author: Xin He
Date: 04 Oct 2007 08:46 AM
Hello all,

I have an xml file which is a list of articles. I need to display the 2 most recent ones, in which one should be from the category of "News" and the other one from any other categories. And they need to be sorted by the publish date. I can display the two items by using

<xsl:for-each select="data/info/article[category!='News']">
<xsl:if test="position()&lt;2">

And

<xsl:for-each select="data/info/article[category=='News']">
<xsl:if test="position()&lt;2">

The problem with this is the two items are now not sorted in publishdate...

Does anyone has an idea? How I can put them back in order by publishdate?

Thanks

Xin

----- xml -----

<?xml version="1.0" encoding="UTF-8"?>
<data>
<info>
<title>News and Blogs</title>
<article>
<title>Title 1</title>
<publishDate>Wed, 03 Oct 2007</publishDate>
<category>Book</category>
</article>
<article>
<title>Title 2</title>
<publishDate>Mon, 01 Oct 2007</publishDate>
<category>News</category>
</article>
<article>
<title>Title 3</title>
<publishDate>Sat, 29 Sep 2007</publishDate>
<category>News</category>
</article>
<article>
<title>Title 4</title>
<publishDate>Fri, 28 Sep 2007</publishDate>
<category>Music</category>
</article>
</info>
</data>

Postnext
James DurningSubject: Display top n items from categories in certain sequence
Author: James Durning
Date: 04 Oct 2007 01:12 PM
I suggest using a template mode instead. Assuming your articles are already pre-sorted:
<xsl:apply-templates select="data/info/article"/>

Then seperate the processing into seperate templates.
<xsl:template match="article[category!='News']">
<!-- contents of your first for-each here -->

<xsl:template match="article[category=='News']">
<!-- contents of your second for-each here -->

Posttop
Xin HeSubject: Display top n items from categories in certain sequence
Author: Xin He
Date: 04 Oct 2007 02:50 PM
Thanks for the idea James.

I tried the template approach but the position() is not working. I need, for example, if the most recent News is in the forth position of the whole list, I still need it to be display since it is the top 1 News.

With the for-each approach, the position() &lt; 2 works because it is relative to the context nodeset.

or am I just not placing it in the right place... please see the xsl and let me know what you think. Thanks a lot !!

<xsl:template match="/">
<xsl:apply-templates select="data/info/article"/>
</xsl:template>

<xsl:template match="article[category!='News']">
<xsl:if test="position()&lt;2">
<xsl:call-template name="singleItem"/>
</xsl:if>
</xsl:template>
<xsl:template match="article[category='News']">
<xsl:if test="position()&lt;2">
<xsl:call-template name="singleItem"/>
</xsl:if>
</xsl:template>

<xsl:template name="singleItem">
<p>
<xsl:value-of select="title"/>
</p>
</xsl:template>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.