The sort xsl does work for the current xsl template but the code moves on and searches the next level independantly and so these results aren't merged and sorted.
i.e. all level 1 nodes get sorted correctly, but sub folders get sorted and added to the end of the level 1 list.
Perhaps there is an easier way to go about this whole issue?
I would also like to only show the top 10 matching nodes but this method of searching doesnt lend itself well to doing this either!
Infact i spoke to soon again! the top 10 section isnt working correctly as position() is just looking at the top 10 articles whether they match to showOnHomepage or not. It needs to be the top 10 that also have showOnHomepage= 1
for example, if I set the position for top 5 instead of 10 (for ease) and have the following list of articles:
yes "//" is abbreviated syntax for "descendant-or-self::".
You can try something like this:
<xsl:template match="/">
<xsl:for-each select="$currentPage//*[@isDoc and (self::article) and showOnHomepage = 1]">
<xsl:sort select="@updateDate" order="descending"/>
<xsl:if test="position() <= 10">
<!-- write out the id of the matching doc -->
<xsl:value-of select="@id" />
</xsl:if>
</xsl:for-each>
</xsl:template>
As you can see i'm not using match templates here any more, instead i'm using <xsl:for-each> to loop through content.
This is more "using XSLT as traditional programming language", and not the "best" way but like majority i learned XSLT that way, and i'm not too familiar in writing XSLT using match templates.
thanks again Almir, thats the way I normally approach xsl and makes much more sense to me... I didn't know there was a descendant-or-self option, learn something new every day.
Looping all site nodes and sorting the results...
Hi guys,
In short my aim is to show a top 10 list of the latest selected articles from my site on the homepage.
I have added a datatype to each article called 'showOnHomepage' and now i want to search all of these and select the 10 latest ones.
I have searched around and have some code used for generating site maps which is finding all of the pages, but I dont know how to sort the results.
My Code so far...
<xsl:template match="/">
<xsl:apply-templates select="$currentPage/*[@isDoc and (self::article)]" />
</xsl:template>
<xsl:template match="*[@isDoc]">
<xsl:if test="showOnHomepage = 1">
<!-- write out the id of the matching doc -->
<xsl:value-of select="@id" />
</xsl:if>
<xsl:if test="*[@isDoc]">
<xsl:apply-templates select="*[@isDoc]" />
</xsl:if>
</xsl:template>
any help would be great!
Cheers
Martz
Hi,
If i understand it right, you can sort them by updateDate. Something like this:
<xsl:apply-templates select="$currentPage/*[@isDoc]">
<xsl:sort select="@updateDate" order="descending"/>
</xsl:apply-templates>
Cheers!
excellent thanks, that seems to work.... templates (and xslt for that matter) are all a bit new to me :-)
Looks like I jumped the gun a little here...
The sort xsl does work for the current xsl template but the code moves on and searches the next level independantly and so these results aren't merged and sorted.
i.e. all level 1 nodes get sorted correctly, but sub folders get sorted and added to the end of the level 1 list.
Perhaps there is an easier way to go about this whole issue?
I would also like to only show the top 10 matching nodes but this method of searching doesnt lend itself well to doing this either!
OK, is this is doing any good:
Yes that works like a dream, thanks very much.
Which part of this makes it search the whole site... is it the '//' in the following section:
$currentPage//*[@isDoc and (self::article)]
as I havent seen that before?
Thanks again
Infact i spoke to soon again! the top 10 section isnt working correctly as position() is just looking at the top 10 articles whether they match to showOnHomepage or not. It needs to be the top 10 that also have showOnHomepage= 1
for example, if I set the position for top 5 instead of 10 (for ease) and have the following list of articles:
article 1 - showOnHomepage = 1
article 2 - showOnHomepage = 0
article 3 - showOnHomepage = 0
article 4 - showOnHomepage = 0
article 5 - showOnHomepage = 1
article 6 - showOnHomepage = 1
article 7 - showOnHomepage = 1
The code will only show articles 1 and 5, as 6 and 7 are after position 5.
Hi Martyn,
yes "//" is abbreviated syntax for "descendant-or-self::".
You can try something like this:
As you can see i'm not using match templates here any more, instead i'm using <xsl:for-each> to loop through content.
This is more "using XSLT as traditional programming language", and not the "best" way but like majority i learned XSLT that way, and i'm not too familiar in writing XSLT using match templates.
Hope it helps.
thanks again Almir, thats the way I normally approach xsl and makes much more sense to me... I didn't know there was a descendant-or-self option, learn something new every day.
is working on a reply...