Quantcast
Viewing all articles
Browse latest Browse all 2

Answer by Dimitre Novatchev for select two items from rss in xslt

Replace this code:

<xsl:template match="/rss/channel"> <div class="RSS_Gadget"> <div class="RSS_Content"> <ul id="widget"> <xsl:apply-templates select="item"    /> <!-- <xsl:apply-templates select="following-sibling::item" mode="extra"    />--> </ul> </div> </div> </xsl:template> <xsl:template match="item"> <li> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="link"/> </xsl:attribute> <xsl:attribute name="target">_blank</xsl:attribute> <xsl:value-of select="title"/> </xsl:element> <div class="RSS_Body"> <xsl:value-of select="description" disable-output-escaping="yes" /> </div> </li></xsl:template> 

with:

<xsl:template match="/rss/channel"> <div class="RSS_Gadget"> <div class="RSS_Content"> <ul id="widget"> <xsl:apply-templates select="item[position() mod 2 = 1]"    /> </ul> </div> </div> </xsl:template> <xsl:template match="item"> <li> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="link"/> </xsl:attribute> <xsl:attribute name="target">_blank</xsl:attribute> <xsl:value-of select="title"/> </xsl:element> <div class="RSS_Body"> <xsl:value-of select="description" disable-output-escaping="yes" /> </div> </li> <xsl:apply-templates select="following-sibling::item[1]" mode="extra"/></xsl:template> 

Explanation:

This instruction:

<xsl:apply-templates select="item[position() mod 2 = 1]"    /> 

Applies templates to the first item element of every pair of two adjacent item siblings (to the 1st, 3rd, ... 2k+1stitem child).

Then in the template that matches an item element, after the matched element is processed another template is applied to/for-processing its immediate following sibling:

<xsl:apply-templates select="following-sibling::item[1]" mode="extra"/>

Note: It is highly probable that you don't need to use DOE (the disable-output-escaping attribute) in your code. Always try to avoid using DOE, because it isn't a mandatory feature of XSLT (not all XSLT processors support and implement it) and its use breaks up the XSLT architectural model.


Viewing all articles
Browse latest Browse all 2

Trending Articles