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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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>

   
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.