如何从xml rss文件中选择两个项目
以下代码一次获得一件物品我希望一次获得两件物品
我应该选择什么来选择第二项
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<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>
答案 0 :(得分:2)
替换此代码:
<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>
<强>与强>:
<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>
<强>解释强>:
这条指令:
<xsl:apply-templates select="item[position() mod 2 = 1]" />
将模板应用于每对两个相邻item
兄弟姐妹的第一个item
元素(1st
,3rd
,... 2k+1st
{ {1}}孩子)。
然后在匹配item
元素的模板中,在处理匹配元素之后,将另一个模板应用于/处理其紧随其后的兄弟:
item
注意:您很可能不需要在代码中使用DOE( <xsl:apply-templates select="following-sibling::item[1]" mode="extra"/>
属性)。总是尽量避免使用DOE,因为它不是XSLT的强制功能(并非所有XSLT处理器都支持和实现它),并且它的使用打破了XSLT架构模型。