tags:

views:

251

answers:

1

I am making a newslist, and have until now sorted the news after their date. Newest first.

But I would like to give the administrator of the site a more flexible solution. This means that in the backend, the admin can choose from a dropdown-list, in wich way he/she want's the list to be sorted. By date(newest first and oldest first), or by title (A-Z and Z-A). This means 4 possible ways right.

Right now I have the following XSLT:

    <xsl:variable name="alleNyheder" select="$currentPage//node" />

    <xsl:variable name="sort">
        <news>
            <xsl:for-each select="$alleNyheder[@template='1092']">
                <news>
                    <id>
                        <xsl:value-of select="./@id"></xsl:value-of>
                    </id>
                    <date>
                        <xsl:choose>
                            <xsl:when test="./data[@alias='date'] != ''">
                                <xsl:value-of select="./data[@alias='date']"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="./@createDate"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </date>
                </news>

            </xsl:for-each>
        </news>
    </xsl:variable>

    <xsl:for-each select="msxml:node-set($sort)/news/news">
        <xsl:sort data-type="text" select="date" order="descending" />

        ---- My newsitems ----
    </xsl:for-each>

So in the moment I sort my list after the "date"-value in my variable $sort.

If I change the "date"-field in $sort, to titles instead of date's I can actually sort my list after the titles of the news. But unfortunately it should be sorted in an ascending order, instead of a descending order. And I can't figure out how to change the order-value dynamically like I do in the select-value.

If it helps anyone, I am working on Umbraco CMS.

Thanks

-Kim

+1  A: 
<xsl:choose>
  <xsl:when test="$sortfield = 'date' and $sortorder = 'D'>
    <xsl:for-each select="msxml:node-set($sort)/news/news">
      <xsl:sort data-type="text" select="date" order="descending" />
      <!-- ... -->
    </xsl:for-each>
  </xsl:when>
  <xsl:when test="$sortfield = 'date' and $sortorder = 'A'>
    <xsl:for-each select="msxml:node-set($sort)/news/news">
      <xsl:sort data-type="text" select="date" order="ascending" />
      <!-- ... -->
    </xsl:for-each>
  </xsl:when>
  <!-- ... -->
</xsl:choose>

On a different note, you really should look into <xsl:apply-templates> and avoid <xsl:for-each>. Your code gets cleaner and a lot more idiomatic this way. I am also sure that the entire temporary node-set() business is completely avoidable.

Tomalak
Hmm..yeah maybe I should try the apply-template. But how would the code then look like? I mean, where would I place the sort then?
Kim Andersen
The `apply-templates` can contain a `sort` as well. In any case it would probably lead to a major rewrite of your script, but the code will be better when you're done.
Tomalak
P.S.: Without seeing your input XML, I can't really suggest how to change your code.
Tomalak
I have now read some different stuff about the apply-templates, and I can see that I actually almost just can change my for-each to apply-templates with the same select. And off cource some with-param and put my content from for-each in a new template. So I will give that a shot tomorrow. Is there anthing specific I should be aware of when I do it? By the way, I also accept your answer as the correct one :) Thanks.
Kim Andersen
Thanks. Well, give it a shot and find out. If you have questions, you know where to ask them. ;-)
Tomalak