I am trying to understand xslt and how it is used in a content query web part. What I want to achieve is getting 5 last items added into a sharepoint list and display them in a special format. The latest item should be displayed in bigger letters and different color, whereas the 4 remaining items will be displayed in smaller font and different color. These items are splitted into two columns, left and right. The last item should be displayed in the left column.

How can I perform a query here and figure out which Item is last and apply different styling and formatting to the rest of the items?

XSLT uses XPath to filter data, but that seems to work only with xml. how about sharepoint list? Can I use caml query inside an xslt to query my data or is it done by XPath? Can you please provide me some examples?

Please it is urgent.

thank you.

1 Answer 1


You can only use CAML query inside a Content Query Web Part by using the Clientside Object Model, but that will call the CAML Query for each and every item displayed. Not a good thing.

XSLT is only used to diplay the results of the Content Query Web Parts queries. Nothing more.

You can at any time create your own XSLT Template within the ItemStyle.xslt which you find in the Style Library on the root web.

You must know that the Content Query Web Part uses 3 different XSLT sheets;

  1. ItemStyle.xslt
  2. ContentQueryMain.xslt
  3. Header.xslt

Which all resides withing the Style Library.

The ContentQueryMain.xslt is the one which has all the templates which does the "pre" work if you can call it that. That's the file which does the filtering, extracting of column URLs etc etc etc.

If you want to have the 5 latest items displayed, that's easily done by configuring the CQWP to sort the items descending, displaying the latest items first. This is default though.

The "tricky" part is to apply a different or slighty different transform on each item depending on the order.

To help you fix this, you must do as follows:

In your ContentQueryMain.xslt find the template OuterTemplate.CallItemTemplate. It has a <xsl:choose> going on in it. In the <xsl:otherwise> clauss add the following:

<xsl:apply-templates select="." mode="itemstyle">
  <xsl:with-param name="CurPos" select="$CurPosition" />

Go to your ItemStyle.xslt and in that either create a new or modify a current template and add this:

<xsl:param name="CurPos"/>

right under

<xsl:template name="MyTemplate" match="Row[@Style='MyTemplate']" mode="itemstyle">

So you should have something like this:

<xsl:template name="MyTemplate" match="Row[@Style='MyTemplate']" mode="itemstyle">
    <xsl:param name="CurPos"/>
    // Rest of your template structure here

This makes you able to choose which item you would like the template to brand differently than others.


<xsl:template name="MyTemplate" match="Row[@Style='MyTemplate']" mode="itemstyle">
    <xsl:param name="CurPos"/> // Gives you the current item position.
    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/> // Used for variable below.
    <xsl:variable name="RowCount" select="count($Rows)"/> // Gives you the last item position.
    <xsl:if test="$CurPos = 1">
        This is the first item the Content Query shows.
    <xsl:if test="$CurPos = $RowCount">
        This is the last item the Content Query shows.
// etc etc etc

Hope this helps :)

  • Hi Dandroid, thanks a lot for your help. One question, if I use a table in the Template section, will it be repeated multiple times? How can I avoid displaying the table multiple times? I want to display it only once, but the data in it should be all displayed.
    – Imir Hoxha
    Nov 11, 2012 at 19:13
  • @IH-77 The table will be repeated multiple times. If you don't want the table to be displayed multiple times, go with DIVs. Whatever HTML you put in your ItemStyle template will be displayed for each item shown in the Content Query Web Part.
    – user2536
    Nov 11, 2012 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.