Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Probocop 51 posts 71 karma points
    Jan 19, 2011 @ 13:31
    Probocop
    0

    Setting the value of xsl:sort with a xsl:choose?

    Hi, I'm using the same macro to list different types of content on different pages, one of the document types has a (mandatory) date field while the other doesn't.

    What I'm trying to do is to check whether the first item has a date set, then use that to sort the list. If it doesn't then just use the @createDate.

    My XSLT is currently as follows, but I get the error of xsl:sort has to be the first item inside the xsl:for-each.

    <xsl:variable name="sortBy">
        <xsl:choose>
          <xsl:when test="contentDate &gt; 0">
            <xsl:text>contentDate</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>@createDate</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:sort select="$sortBy" order="descending" />

    Can anybody think of a way of doing this?

    Thanks!

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Jan 19, 2011 @ 14:07
    Lee Kelleher
    1

    Hi Probocop,

    You can't use the variable's value directly in the sort's select - as that requires an XPath expression... so try this instead:

    <xsl:variable name="sortBy">
        <xsl:choose>
            <xsl:when test="contentDate &gt; 0">
                <xsl:text>contentDate</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>@createDate</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:apply-templates select="$currentPage">
        <xsl:sort select="contentDate[$sortBy = 'contentDate']" order="descending" />
        <xsl:sort select="@createDate[$sortBy = '@createDate']" order="descending" />
    </xsl:apply-templates>

    It's a little dirty workaround that Chriztian taught me! ;-)

    Cheers, Lee

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies