Unlocking the Mysteries of Data View Web Part XSL Tags – Part 8: xsl:sort
Author: Marc D. Anderson
http://mdasblog.wordpress.com
<xsl:sort>
Used within an <xsl:for-each> to determine the sort order in which the nodeset (group of rows) are processed.
<xsl:sort> allows you to change the default sort order of the items as they are displayed in a Data View Web Part (DVWP). By default (in almost all cases), the items will be displayed in ID number order. This is rarely useful, so in most cases you will want to indicate your own sort order.
<xsl:sort> is used within the <xsl:for-each> tag. This is because the sorting occurs on the entire rowset before the individual rows are displayed.
In the simple example we’ve been using, you don’t see an <xsl:sort>. This is because when you first add a DVWP to the page, you haven’t yet specified a sort order:
<XSL><xsl:stylesheet xmlns:x="<a href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</a>" xmlns:d="<a href="http://schemas.microsoft.com/sharepoint/dsp">http://schemas.microsoft.com/sharepoint/dsp</a>" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">http://schemas.microsoft.com/WebParts/v2/DataView/runtime</a>" xmlns:asp="<a href="http://schemas.microsoft.com/ASPNET/20">http://schemas.microsoft.com/ASPNET/20</a>" xmlns:__designer="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/designer">http://schemas.microsoft.com/WebParts/v2/DataView/designer</a>" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal"> <xsl:output method="html" indent="no"/> <xsl:decimal-format NaN=""/> <xsl:param name="dvt_apos">'</xsl:param> <xsl:variable name="dvt_1_automode">0</xsl:variable> <xsl:template match="/"> <xsl:call-template name="dvt_1"/> </xsl:template> <xsl:template name="dvt_1"> <xsl:variable name="dvt_StyleName">Table</xsl:variable> <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/> <table border="0" width="100%" cellpadding="2" cellspacing="0"> <tr valign="top"> <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1"> <th width="1%" nowrap="nowrap"></th> </xsl:if> <th nowrap="nowrap">Title</th> </tr> <xsl:call-template name="dvt_1.body"> <xsl:with-param name="Rows" select="$Rows"/> </xsl:call-template> </table> </xsl:template> <xsl:template name="dvt_1.body"> <xsl:param name="Rows"/> <xsl:for-each select="$Rows"> <xsl:call-template name="dvt_1.rowview"/> </xsl:for-each> </xsl:template> <xsl:template name="dvt_1.rowview"> <tr> <xsl:if test="position() mod 2 = 1"> <xsl:attribute name="class">ms-alternating</xsl:attribute> </xsl:if> <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1"> <td width="1%" nowrap="nowrap"> <span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span> </td> </xsl:if> <td> <xsl:value-of select="@Title"/> </td> </tr> </xsl:template> </xsl:stylesheet></XSL>
If you do want to specify a sort order, you add the <xsl:sort> within the <xsl:for-each>, as I mentioned above. In this example, the rows (items) will just be sorted by the values in the Title column:
<xsl:template name="dvt_1.body"> <xsl:param name="Rows"/> <xsl:for-each select="$Rows"> <xsl:sort select="@Title" /> <xsl:call-template name="dvt_1.rowview"/> </xsl:for-each> </xsl:template>
In the simplest case, you just add the column(s) you want to sort by and you are all set. However, there are several other attributes it’s important to know about:
Attribute
|
Values |
Description |
select |
XPath expression |
The select can take the form of any value XPath expression. Most often, you’ll just specify a column name. Sometimes you may want to sort by a calculated value or a value in another list, and you can specify those expressions as well. |
order |
[ascending | descending] |
This setting determines whether the sort is A-Z or Z-A. |
data-type |
[text | number] |
Data-type lets you specify what type of value the select contains. This is most often useful when you’d like to treat numbers as numbers rather than as text. |
lang |
language-code |
Specify the language if you want the sort to be “language-aware”. |
case-order |
[upper-first | lower-first] |
In the instance where the values are of mixed case, this setting identifies how to treat capital vs. lower case characters. |
I’ve never had the need to specify the lang or case-order attributes myself, but it’s good to know about them.
You can have multiple <xsl:sort> tags in your <xsl:for-each>. The <xsl:sort> tags are simply executed in order, giving you the ability to have primary, secondary, tertiary, etc., sorts. For instance, the following example shows how you might generate a list of event attendees, sorted by LastName and then FirstName so that you could take attendance as people arrive:
<xsl:template name="dvt_1.body"> <xsl:param name="Rows"/> <xsl:for-each select="$Rows"> <xsl:sort select="@LastName" /> <xsl:sort select="@FirstName" /> <xsl:call-template name="dvt_1.rowview"/> </xsl:for-each> </xsl:template>
If you want to sort by a value that is in a different list, you need to specify the appropriate XPath expression. At this point, you’re leaving the warm and comfy zone of the SharePoint Designer dialogs and you’ll need to write the XPath expression yourself. Let’s say that you have two lists, one with one item per sale, and the other with total sales by region. Assuming that you’ve set up your DataSource to contain both lists (you can have many more than two lists in a DVWP), the XPath expression might look something like this:
<xsl:sort select="/dsQueryResponse/List2/Rows/Row[@Region = current()/@Region]/@TotalSales" order=”descending” />
This will display the individual sales sorted by the highest sales per region.
Next up: <xsl:if>
A single conditional test. If the test is true, then the contained code is executed.
Author: Marc D. Anderson
http://mdasblog.wordpress.com
Marc D. Anderson is a Co-Founder and the President of Sympraxis Consulting LLC, based in Newton, MA. He has over 25 years of experience as a technology consultant and line manager across a wide spectrum of industries and organizational sizes. Marc has done extensive consulting on knowledge management and collaboration and what makes them actually work in practice. Marc is a very frequent “answerer” on the MSDN SharePoint – Design and Customization forum.
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 1: Overview
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 2: xsl:template
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 3: xsl:call-template
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 4: xsl:with-param
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 5: xsl:param
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 6: xsl:variable
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 7: xsl:for-each
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 8: xsl:sort
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 9: xsl:if
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 10: xsl:choose
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 11: xsl:value-of
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 13: Miscellaneous - String Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16: Miscellaneous – xsl:attribute
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17: Miscellaneous – xsl:comment and xsl:text
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20: xsl:import
- EUSP eBook Store: First SharePoint Title is Now Available
I like the warm and comfy world of the SharePoint Designer dialogs. =) It’s familiar and productive. I wander out into the XSLT jungle as needed … but try to watch my step carefully. Great article as always, thank you Marc!
If more comments had the journalistic prose as yours do, we might actually start a discussion group. Nicely stated. — Mark
Hi,
I am showing two fields in Data View webpart and it works all good except that I want only only first 80 characters of one of the field to be shown rather than the entire field text. Is there a way to achieve this…?
@Nisha:You can show just the first 80 characters like this:
M.
Thanks Marc….Bless You…!!
One more question…the kind of webpart connection we have for two webparts. Can we have it all in a single Data view webpart. I mean data shown from two list where one list will send parameters to other. I just want to avoid trouble of moving two webparts for customer. Thanks in advance
@Nisha: Yes, you can access multiple lists in the same DVWP with an AggregateDataSource. I do it all the time. It’s basically what you get when you create a DVWP with a Linked Data Source. It’s not really a “join” (I always put it in quotes), but just filtering on the rowsets for each DataSource.
M.
Hi Marc,
I apologise for in advcance for asking dumb question but I started playing with XSLT recently so please pardon my ignorance. I created Data View webpart showing the latest blog and its comment. Now the only problem is the link on the title of the blog doesnt take the user to the real blog, it just sits there. Can you please advice. Under given is the piece of code which I think I need to modify but not sure what modification.
@Title=
javascript:
@Nisha: No question is dumb unless you ask it more than 5 times! I think it’s better to post these types of questions to Stump the Panel, though, since they aren’t really about this article. You’ll also be able to post code there without difficulty.
M.
Thank you Marc :-)…I just made the post there.
Hi Marc,
Please save my life.
I have two connected webparts. Everything works perfect but the only problem is webpart which takes the value from other webpart doesnt let any webpart sit under it. I automatically moves all the webparts under it to some other webpart zone. What is it that I should be looking at? Please help
Thanks
Nisha
Nisha:
This is another one for Stump the Panel, as it doesn’t really have anything to do with this post. I think you’ll need to give some more information, too, as I’m not sure what your issue is.
Thanks,
M.
I am trying to use a DVWP to display data from a SQL Server db. I have tried using stored procs, a simple select within the DVWP, I have also created a view in SQL Server and configured my connection to use that view. No matter which method I use, the data rreturns properly but the sorting and grouping UI dropdowns do not function properly. Any ideas what may be wrong?
Different DataSources can behave differently in DVWPs. You should post this question in Stump the Panel (link at the top of the page). You might get some more ideas from a wider audience.
What it’s going to come down to is the XSL in the DVWP. You may need to build something up yourself in the situation you are describing. (I haven’t done much with SQL DataSources myself.)
M.
Thank you very much Marc for your time and input. I will post it to the Stump the Panel forums and see what response I get.