Copied to clipboard

Flag this post as spam?

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


  • umbracocool 108 posts 197 karma points
    Apr 10, 2012 @ 20:13
    umbracocool
    0

    Problems to sort in xslt

    Hello friends

    I'm having trouble sorting a list of products, which are sorted in ascending, descending, by date, most recent, etc ... I'm trying to do this,

    but I get an error.

    Help please!.Thank you!

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 10, 2012 @ 20:19
    Tom Fulton
    0

    Hi,

    Check out this gist from Chriztian, think it does exactly what you need (just change @createDate to @nodeName).

    -Tom

  • umbracocool 108 posts 197 karma points
    Apr 10, 2012 @ 22:04
    umbracocool
    0

     

    Hi Tom, thanks for answering. I've been testing the sample you sent me, but I do not work yet. Can you help some more? to see if I can solve this problem, I'm not very expert in Umbraco XSLT

    This is what I have:

     

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 10, 2012 @ 22:45
    Tom Fulton
    0

    Hi,

    Try removing the xsl:choose and xsl:when statements and just list the sort statements - if you look at the gist I linked it's not using them.  It's deciding which statements to use via the predicates ($createDate [$page = 'desc']) - if $page = asc this statement will be ignored.  At least I'm pretty sure that's how it works :)

    -Tom

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 10, 2012 @ 23:59
    Chriztian Steinmeier
    1

    Hi umbracocool,

    The main reason your code won't work is because xsl:sort is only allowed inside xsl:for-each and xsl:apply-templates  - but there's another very tricky problem which you can stare at all day long until you suddenly see it:

    You need to know that a select attribute always takes an XPath expression, so what your $page variable is actually set to is the value of a <byNameAsc> element in a context you can't even get access to – you just want it to be a string, so you need to put some single-quotes around it:

    <xsl:variable name="page" select="'byNameAsc'" />

    With that out of the way, rewrite th rest to something like this:

    <xsl:for-each select="$currentPage/*[@isDoc]">
        <xsl:sort select="@createDate[$page = 'desc']" order="descending" />
        <xsl:sort select="@createDate[$page = 'asc']" order="ascending" />
        <xsl:sort select="@nodeName[$page = 'byNameAsc']" order="ascending" />
        <xsl:sort select="@nodeName[$page = 'byNameDesc']" order="descending" />
    
        <p>
            Page: <xsl:value-of select="@nodeName" />, <xsl:value-of select="@createDate" />
        </p>
    
    </xsl:for-each>

    (data-type defaults to "text" so I removed it to reduce clutter)

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 11, 2012 @ 00:00
    Chriztian Steinmeier
    0

    - and Tom: You're right about how it works :)

  • umbracocool 108 posts 197 karma points
    Apr 11, 2012 @ 00:49
    umbracocool
    0

     

    Yupiii! Thank you very much friends, the code has worked for me perfectly, I am eternally grateful to you both for your time and ezfuerso. A Tom and Chriztian a hug.

    Before this I would like to clarify that static works very well:

    <xsl:variable name="page" select="'byNameAsc'"/>

    But if you want the value of $ page, a dynamic Through of the url ex:

    http://mysite.com.do/?order=desc

    This would be the best solution:

     

      <xsl:variable name="order" select="umbraco.library:RequestQueryString('order')"/>
      <xsl:variable name="page" select="string($order)"/>

    My final code is this:

     

     

      <xsl:variable name="order" select="umbraco.library:RequestQueryString('order')"/>
      <xsl:variable name="page" select="string($order)"/>
     <xsl:for-each select="$currentPage/* [@isDoc]">
    <xsl:sort select="@createDate[$page = 'desc']" data-type="text" order="descending" />
    <xsl:sort select="@createDate[$page = 'asc']" data-type="text" order="ascending" />
    <xsl:sort select="@nodeName[$page = 'byNameAsc']" data-type="text" order="ascending" />
    <xsl:sort select="@nodeName[$page = 'byNameDesc']" data-type="text" order="descending" />
    </xsl:for-each>

    Thank you both! I served very helpful! :)

     

     

     

  • 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