sequential transformations
I'm working on a content management system which pulls blog posts from MySQL and automagically turns them into XML data. This works. I've written XSLT code to sort the posts in descending or ascending order (determined by a parameter fed in via PHP) by date, then group the posts by date (i.e., posts written the same day appear under a common heading). This works.
Now I need to limit the number of posts displayed (current XSLT returns all posts). I'm fairly certain the recursive for-eaches used by the grouping code rule out doing this while grouping (although finding out I'm wrong about this would be a welcome discovery). So I think the only option is to apply the grouping template, then apply a second, post-limiting template to the result.
After reading quite a bit about result tree fragments, variables, and functions, I've yet to find any coherent sample XSLT that reprocessed results from another template. Perhaps part of the problem is that I don't know what to call what I'm trying to do and haven't found a Googleable term. Am I missing something glaringly obvious?
Here's my code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="order" />
<xsl:param name="max" />
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="posts"/>
</body>
</html>
</xsl:template>
<xsl:key name="posts-by-date" match="post" use="date" />
<xsl:template match="posts">
<xsl:for-each select="post[count(. | key('posts-by-date', date)[1]) = 1]">
<xsl:sort select="timestamp" order="{$order}" />
<h2><xsl:value-of select="date" /></h2>
<xsl:for-each select="key('posts-by-date', date)">
<xsl:sort select="timestamp" order="{$order}" />
<h3><xsl:value-of select="title" /></h3>
<xsl:value-of select="parent::node[count()]" />
<xsl:value-of select="body" />
(<xsl:value-of select="time" />)
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Thanks in advance. Note: I'm not limited to XSLT 1.0. Or at least I don't think so...
|