0

I'm basically editing ItemStyle.xsl to get it to retrieve what I need and display through a CQWP (Content Query Web Part). I need it to display only the top 5 posts with the most comments. The comments can be retrieved via @NumComments. I'm not familiar enough with XSL to know how to do it, I'm assuming using count? Any tips?

Here's the current XSL code for that template which just displays all posts.

<xsl:template name="MostCommented" match="Row[@Style='MostCommented']" mode="itemstyle">
    <xsl:variable name="SafeLinkUrl">
        <xsl:call-template name="OuterTemplate.GetSafeLink">
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="DisplayTitle">
        <xsl:call-template name="OuterTemplate.GetTitle">
            <xsl:with-param name="Title" select="@Title"/>
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <div>
        <a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
            <xsl:value-of select="$DisplayTitle"/>
        </a>
    </div>
</xsl:template>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
zen
  • 1,115
  • 3
  • 28
  • 54

2 Answers2

1

You need to add this code -- within a template that matches the parent of Row

<xsl:apply-templates select="Row[@Style='MostCommented']" mode="itemstyle">
 <xsl:sort select="@NumComments" data-type="number" order="descending"/>
</xsl:apply-templates>

Also, inside your template (the one that will be selected for execution as result of the xsl:apply-templates above) wrap all existing code in a conditional as shown below:

<xsl:template name="MostCommented" match="Row[@Style='MostCommented']" mode="itemstyle">
 <xsl:if test="not(position() > 5)">
  <!-- Put all already-existing code here -->
 </xsl:if>
</xsl:template> 

Here is a complete example, illustrating this technique:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <t>
       <xsl:apply-templates select="*">
        <xsl:sort select="@valued"
            data-type="number" order="descending"/>
       </xsl:apply-templates>
     </t>
 </xsl:template>

 <xsl:template match="post">
  <xsl:if test="not(position() >5)">
   <xsl:copy-of select="."/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document (as you forgot to provide one !!!):

<t>
 <post valued="5"/>
 <post valued="2"/>
 <post valued="9"/>
 <post valued="8"/>
 <post valued="6"/>
 <post valued="3"/>
 <post valued="4"/>
 <post valued="10"/>
 <post valued="2"/>
 <post valued="7"/>
</t>

the wanted, correct result is produced:

<t>
   <post valued="10"/>
   <post valued="9"/>
   <post valued="8"/>
   <post valued="7"/>
   <post valued="6"/>
</t>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Dimitre, I'm unsure of where to add the apply-templates part. Would it be in the same document? – zen Dec 27 '11 at 15:05
  • @zen: Somewhere in the code you haven't shown to us there is an `xsl:apply-templates` instruction that selects for execution the template that you have provided in your question. It may be written in many different ways so I can't tell you how to locate this instruction, without seeing the complete code and without seeing the source XML document. You don't have to add a new `xsl:apply-templates` instruction, only change the current `` to ` – Dimitre Novatchev Dec 27 '11 at 17:13
  • ok thanks I'll look for it. It's SharePoint, so a lot of the stuff is inaccessible or hidden, etc. – zen Dec 27 '11 at 17:33
  • @zen: The XSLT code is not hidden. Also, you can get the XML document by a simple `` – Dimitre Novatchev Dec 27 '11 at 17:46
0

I was able to do this directly in the CQWP web part instead of the ItemStyle.xsl

Replacing

<property name="QueryOverride" type="string" />

With

<property name="QueryOverride" type="string"><![CDATA[<OrderBy><FieldRef Name="NumComments" Nullable="True" Type="Lookup" Ascending="False"/></OrderBy>]]></property>
zen
  • 1,115
  • 3
  • 28
  • 54