2012-06-06 130 views
1

我得把前4條評論下令從這個XML所有評論下降(由DATE_ADDED訂購):排序嵌套評論

的,我想最後的4條評論輸出
<?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> 

例:

admin1 : nice : 20.06.2005 
admin1 : comment1 : 19.05.2005 
another : comment3 : 15.05.2005 
admin2 : too nice : 11.05.2005 

完美的作品,如果我讓他們作爲項目的列表-comments-,但不經過我一個新的標籤下把它們分開

<product id=""> comments </product> 

我不能對它們進行排序所有,並採取第一4.它是如何工作的,如果我有dont't的「產品」的標籤,我的意思是所有的評析具有與父「產品」標籤:

<?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() &lt; 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&amp;id={$commID}"> 
     <xsl:value-of select="substring(text,0,60)" /> 
    </a><br/> 

</xsl:for-each> 

</xsl:template> 
</xsl:stylesheet> 
+1

目前尚不清楚你想要達到的目標。請編輯您的文章並顯示您想要從輸入中獲得的輸出。另外,在您的輸入中,''下的第一個嵌套標記也是''...應該是''而不是? –

回答

1

不確定你想要做什麼,但嘗試使用xsl:apply-templates而不是xsl:for-each

例子:

XSLT 1.0

<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:apply-templates select="product/comment"> 
      <xsl:sort select="concat(
       normalize-space(substring(date_added,7,4)), 
       normalize-space(substring(date_added,4,2)), 
       normalize-space(substring(date_added,1,2)) 
       )" data-type="number" order="descending" /> 
     </xsl:apply-templates>  
    </xsl:template> 

    <xsl:template match="comment"> 
     <!--xsl:if used because it wasn't working on the predicate?--> 
     <xsl:if test="4 >= position()"> 
      <a href="index.php?p=comment&amp;id={@id}"> 
       <xsl:value-of select="substring(text,1,60)" /> 
      </a><br/> 
      <xsl:text>&#xA;</xsl:text>     
     </xsl:if> 
    </xsl:template>  

</xsl:stylesheet> 

使用XML輸入產生:

<a href="index.php?p=comment&amp;id=1">nice</a><br> 
<a href="index.php?p=comment&amp;id=1">comment1</a><br> 
<a href="index.php?p=comment&amp;id=3">comment3</a><br> 
<a href="index.php?p=comment&amp;id=2">too nice</a><br> 

您的href的需要修改,如果您有重複的評論的ID跨產品(如你在你的例子中有XML)。

(編輯:。錯過了,你試圖返回頂部4評論修復還修復了3 substring()錯字)

+0

正是我想要的!謝啦! –

+0

@DanielG - 沒問題!很高興我能幫上忙 :-) –