Xslt XSL解析和按日期排序
我有一个用于呈现RSS提要的现有xsl。Xslt XSL解析和按日期排序,xslt,xml-parsing,Xslt,Xml Parsing,我有一个用于呈现RSS提要的现有xsl。 除了标题,其他所有内容都从描述字段中解析出来。 下面的代码片段是目前如何完成的。它可以工作,但没有设置项目的顺序。 如何修改现有内容,使项目按itemPubDate排序。 示例itemPubDate值为06/26/2013 <xsl:template name="RSSMainTemplate.body"> <xsl:param name="Rows" /> <xsl:param name="RowCount
除了标题,其他所有内容都从描述字段中解析出来。
下面的代码片段是目前如何完成的。它可以工作,但没有设置项目的顺序。 如何修改现有内容,使项目按
itemPubDate
排序。
示例itemPubDate
值为06/26/2013
<xsl:template name="RSSMainTemplate.body">
<xsl:param name="Rows" />
<xsl:param name="RowCount" />
<xsl:for-each select="$Rows">
<xsl:variable name="CurPosition" select="position()" />
<xsl:variable name="RssFeedLink" select="$rss_ID" />
<xsl:variable name="CurrentElement" select="concat($RssFeedLink,$CurPosition)" />
<xsl:call-template name="RSSDescTransform1">
<xsl:with-param name="DescriptionField" select="description" />
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="RSSDescTransform1">
<xsl:param name="DescriptionField" />
<xsl:variable name="itemTitle" select="title" />
<xsl:variable name="itemAuthor" select="substring-before(substring-after($DescriptionField, 'itemAuthor:</b> '),'</div>')" />
<xsl:variable name="itemPubDate" select="substring-before(substring-after($DescriptionField, 'itemPubDate:</b> '),'</div>')" />
<xsl:variable name="itemDescription" select="substring-before(substring-after($DescriptionField, 'itemDescription:</b> '),'</div>')" />
<xsl:variable name="itemHTML" select="substring-after($DescriptionField, 'itemHTML:</b> ')" />
<xsl:variable name="itemLink" select="substring-before(substring-after($DescriptionField, 'href="'),'"')" />
<xsl:call-template name="RSSDescTransform2">
<xsl:with-param name="itemTitle" select="$itemTitle" />
<xsl:with-param name="itemAuthor" select="$itemAuthor" />
<xsl:with-param name="itemPubDate" select="$itemPubDate" />
<xsl:with-param name="itemDescription" select="$itemDescription" />
<xsl:with-param name="itemLink" select="$itemLink" />
<xsl:with-param name="itemHTML" select="$itemHTML" /></xsl:call-template>
</xsl:template>
完整XSL
我目前没有输入的样本,但它看起来像
<item>
<title>lorem ipsum</title>
<description>
<![CDATA[
<div>
itemAuthor:<b>John</b>
itemPubDate:<b>06/26/2013</b>
...
</div>
]]>
<description>
</item>
<item>
...
</item>
乱数假文
项目作者:约翰
发布日期:2013年6月26日
...
]]>
...
输出几乎相同,只是使用了一些CSS样式
XSL现在不会导致任何错误。我只想按
itemPubDate
对项目进行排序。您应该能够在RSSMainTemplate.body模板中的for each中应用排序。
关键是提取日期片段(年、月、日),并分别对每个片段进行排序
<xsl:for-each select="$Rows">
<xsl:sort select="substring(substring-before(substring-after(description, 'itemPubDate:<b>'),'</b>'),7,4)" />
<xsl:sort select="substring(substring-before(substring-after(description, 'itemPubDate:<b>'),'</b>'),4,2)" />
<xsl:sort select="substring(substring-before(substring-after(description, 'itemPubDate:<b>'),'</b>'),1,2)" />
我还注意到select语句中有一个问题,即从div(基于示例XML)中提取信息,在div中
<xsl:variable name="itemPubDate" select="substring-before(substring-after($DescriptionField, 'itemPubDate:</b> '),'</div>')" />
应该是
<xsl:variable name="itemPubDate" select="substring-before(substring-after($DescriptionField, 'itemPubDate:<b>'),'</b>')" />
如果没有输入XML和输出示例,很难说问题出在哪里。您可以将
与
一起使用,但不能与call一起使用,而且您有两个级别的
这一事实让我觉得XSL还存在其他问题。@Legostrmtroppr添加了其他信息这很有效,谢谢!实际上select语句是正确的,我只是输入错误。它应该看起来像itemPubDate:22/07/2013
。我相应地修改了你的答案。