Wednesday, November 23, 2005

XSLT for trimming RSS results

Recently I was working on a project where there was a requirement to display only a specified number of items returned by a RSS Feed (instead of all the items).

This XSLT will trim the results of an RSS Feed. Set appropriate value to "resultcount" parameter.

<?altova_samplexml C:\junk\rss.xml?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="resultcount">2</xsl:param>
<!-- Identity transform, passes nodes through untouched -->
<xsl:template match="@*|node()|text()">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()"/>
</xsl:copy>
</xsl:template>
<!-- Match the item nodes. -->
<xsl:template match="item">
<!--
<position><xsl:value-of select="count(preceding::item)"/></position>
-->
<xsl:if test="count(preceding::item) < $resultcount">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()"/>
</xsl:copy>
</xsl:if>

</xsl:template>
</xsl:stylesheet>

No comments: