我必须从这个 xml 中的所有评论中获取前 4 条评论,(按 date_add 排序):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="get7Comments.xsl"?>
<products>
<product id="1">
<comment id="1">
<username>admin1</username>
<text>nice</text>
<date_added>20.06.2005</date_added>
</comment>
<comment id="2">
<username>admin2</username>
<text>too nice</text>
<date_added>11.05.2005</date_added>
</comment>
</product>
<product id="2">
<comment id="1">
<username>admin1</username>
<text>comment1</text>
<date_added>19.05.2005</date_added>
</comment>
<comment id="2">
<username>daniel</username>
<text>comment2</text>
<date_added>06.05.2005</date_added>
</comment>
<comment id="3">
<username>another</username>
<text>comment3</text>
<date_added>15.05.2005</date_added>
</comment>
</product>
</products>
我想要的最后 4 条评论的输出示例:
admin1 : nice : 20.06.2005
admin1 : comment1 : 19.05.2005
another : comment3 : 15.05.2005
admin2 : too nice : 11.05.2005
如果我让它们作为项目列表 - 评论 - 完美工作,但不是在我将它们分隔在新标签下之后
<product id=""> comments </product>
我无法将它们全部排序并取第 4 个。如果我没有“产品”标签,它是如何工作的,我的意思是所有评论都作为父“产品”标签:
<?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" encoding="utf-8"/>
<xsl:template match ="products">
<xsl:for-each select="product[position() < 8]">
<xsl:sort select="normalize-space(substring(date_added,7,4))" order="descending" />
<xsl:sort select="normalize-space(substring(date_added,4,2))" order="descending" />
<xsl:sort select="normalize-space(substring(date_added,0,2))" order="descending" />
<xsl:variable name="commID" select="@id" />
<a href="index.php?p=comment&id={$commID}">
<xsl:value-of select="substring(text,0,60)" />
</a><br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>