Not yet rated

Problem

You have created an XSLT fragment to access an RSS feed or XML document, but you don't want all items to be displayed. You want to show just a selected number of items from the top of the list.

Solution

Use an XSLT conditional region to specify the number of items to be displayed.

Detailed explanation

The XSL Transformations server behavior in Dreamweaver 8 and later makes it easy to create an XSLT fragment that can be used to display the results of an RSS feed in a dynamic web page. However, the default repeat region offers no way to restrict the number of items displayed. To control the number of items, all that's needed is to add an XSLT conditional region.

Assuming that your XSLT repeat region contains the title and description nodes of an RSS feed, the code in your XSLT fragment will look something like this:

<xsl:for-each select="rss/channel/item">
  <h3><a href="{link}"><xsl:value-of select="title"/></a></h3>
  <p><xsl:value-of select="description" disable-output-escaping="yes"/></p>
</xsl:for-each>

 

In Code view, select everything between the <xsl:for-each> tags (in other words, the second and third lines of the preceding code sample).

Click the Conditional Region button in the XSLT category/tab of the Insert panel/bar (or select Insert > XSLT Objects > Conditional Region), and type the following code in the Test field of the dialog box that opens:

position() <= 5

This limits the repeat region to showing the first five items. Adjust the number to suit your needs.

When you click OK, the code in your XSLT fragment will have changed like this:

<xsl:for-each select="rss/channel/item">
  <xsl:if test="position() &lt;= 5">
    <h3><a href="{link}"><xsl:value-of select="title"/></a></h3>
    <p><xsl:value-of select="description" disable-output-escaping="yes"/></p>
  </xsl:if>
</xsl:for-each>

 

Dont worry that Dreamweaver has changed the lesser than sign ( <) to its HTML equivalent ( &lt;). That's the correct way to define "lesser than" in XSLT.

When you test the XSLT fragment, it should now display the correct number of items. If it doesn't work, check that you have added a pair of parentheses after position. It's a function, and won't work without the parentheses.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes