1 minute read
This is something that I’ve tried to solve many times and each time I’ve ended up doing something more complicated than I’ve needed to, but I knew would work. I finally bit the bullet and solved it the right way.
Here’s the scenario:
- You have a primary list with many items in it
- One of the columns in this list is a lookup column into another list
- The second list also has a column called SortOrder, which contains a number indicating where in the sort order the lookup value ought to be displayed
- When you display the items from the first list, you want to sort them by the corresponding SortOrder value in the second list
Let’s run through the scenario with these two lists:
In your DWVP, you’ll have a template to gather the right rows from the primary list, then an xsl:for-each where you will do the sort. The xsl:sort needs to look something like this:
<xsl:sort select=”/dsQueryResponse/secondary_list/Rows/Row[@Color = current()/@Color]/@SortOrder”/>
Assuming that you have a secondary sort of the Title column (<xsl:sort select=”@Title”/>), the results will be returned like this:
- Title D
- Title C
- Title A
- Title B
Simple little line, but it was a dickens to figure out the exact syntax. Here’s how it breaks down:
The /dsQueryResponse/secondary_list/Rows/Row says to go and get the rows from the secondary list. Anything within brackets filters that result set, so with [@Color = current()/@Color] we’re saying rows where the Color in the secondary_list equals the Color column in the current result set (the result set from the primary_list). Finally, the /@SortOrder grabs the value in the SortOrder column.
For all of this to work properly, you need to know that your data model will support this – you want to be sure that there is only one row per Color in the secondary_list. You also have to have your DVWP set up with an AggregateDataSource containing both lists (in this case, they are named primary_list and secondary_list in the DataSource).