Sort nested comments after date in XSL

konphygorac

New Member
I have to take first 4 comments ordered descending from all comments in this xml, (ordered by date_added):\[code\]<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="http://stackoverflow.com/questions/10923907/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>\[/code\]Example of output for the last 4 comments that I want:\[code\]admin1 : nice : 20.06.2005admin1 : comment1 : 19.05.2005another : comment3 : 15.05.2005admin2 : too nice : 11.05.2005\[/code\]Works perfectly if I let them as a list of items -comments-, but not after I separate them under a new tag \[code\]<product id=""> comments </product> \[/code\]I can't sort them all and take first 4. How it works if I dont't have the 'product' tag, I mean all coments have as the parent 'products' tag :\[code\]<?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="http://stackoverflow.com/questions/10923907/index.php?p=comment&id={$commID}"> <xsl:value-of select="substring(text,0,60)" /> </a><br/></xsl:for-each> </xsl:template> </xsl:stylesheet>\[/code\]
 
Back
Top