Copied to clipboard

Flag this post as spam?

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


  • Chad 65 posts 129 karma points c-trib
    Oct 15, 2009 @ 03:47
    Chad
    0

    XSLT Conditional Sorts...

    Ok I've posted a thread on this previously, and got some great help - but requirements have changed and I'm still not able to get this to work correctly. The previous thread has been marked as 'answered' and it seems I can't undo that (or edit posts..) and it's not getting much love anymore.

     

    Here's the situation:

       <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <xsl:sort select="./data [@alias = 'colourCode']" />

     

    This works fine, but I want so that in the presense of a querystring (or similar), it sorts by the @nodeName instead.

     

  • Shannon Deminick 1524 posts 5270 karma points MVP 2x
    Oct 15, 2009 @ 04:41
    Shannon Deminick
    3

    You should create a xsl:variable to store your sort parameter with a choose clause

    <xsl:variable name="sortParam">
          <xsl:choose>
            <xsl:when test="TESTFORQUERYSTRING">
              <xsl:value-of select="yourquerystringval"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="yournormalval"/>
            </xsl:otherwise>
          </xsl:choose>

        </xsl:variable>   
        <xsl:for-each select=".">
          <xsl:sort select="$sortParam"/>
          ...
        </xsl:for-each>

  • Chad 65 posts 129 karma points c-trib
    Oct 15, 2009 @ 06:10
    Chad
    0

    I'm doing this wrong.

    This is no dice.... (excuse the dodgy conditional logic, was just testing):

    <xsl:variable name="sortParam">
    <xsl:choose>
    <xsl:when test="1 = 2">
    <xsl:value-of select="@nodeName"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="$currentPage/node/data [@alias = 'colourCode']"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <xsl:sort select="$sortParam" />
    .....

    That's not going to work, as it's grabbing the the values of the first node, not of each node.

    I've tried using <xsl:text> tag (instead of xsl:value-of, the idea being, I want to set the xpath(?) statement to be used for the sort itself) but no luck there either.

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Oct 15, 2009 @ 09:23
    Warren Buckley
    0

    Chad I am not entirely sure what you want to do?

    So if a querystring is present you want to sort by @nodeName if not you want to sort by the property colourCode ?

    Warren

  • Jacob Jensen 29 posts 49 karma points
    Oct 15, 2009 @ 09:56
    Jacob Jensen
    0

     <xsl:variable name="content" select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']" mode="content"/>


     <xsl:choose>

      <xsl:when test="1=2">

       <xsl:apply-templates select="$content">
     
       <xsl:sort select="@nodeName" />

       </xsl:apply-templates>

      </xsl:when>  
      
      <xsl:otherwise>

       <xsl:apply-templates select="$content">

        <xsl:sort select="@nodeName" />

       </xsl:apply-templates>
      
      </xsl:otherwise>
     
     </xsl:choose>


     </xsl:template>


     <xsl:template match="node" mode="content">

     <!-- Copy your <xsl:foreach> content here -->
     </xsl:template>

  • Jacob Jensen 29 posts 49 karma points
    Oct 15, 2009 @ 09:58
    Jacob Jensen
    0

     hmm forgot to sort by

    $currentPage/node/data [@alias = 'colourCode']

    in <xsl:otherwise>

  • Jacob Jensen 29 posts 49 karma points
    Oct 15, 2009 @ 10:05
    Jacob Jensen
    0

    (you cannot sort by an variable holding a node-set, just text-strings like data[@alias = $SortParam])

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Oct 15, 2009 @ 21:15
    Chriztian Steinmeier
    0

    Because you can't use a variable in the select attribute of the xsl:sort element, you'll need to branch out  - here's how I'd do it:

       <xsl:variable name="sortByNodeName" select="boolean(umb:RequestQueryString('sortbyname') = 'yes')" />
    
        <xsl:template match="/">
            <ol>
                <xsl:choose>
                    <xsl:when test="$sortByNodeName">
                        <xsl:apply-templates select="$currentPage/node">
                            <xsl:sort select="@nodeName" data-type="text" order="ascending" />
                        </xsl:apply-templates>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="$currentPage/node">
                            <xsl:sort select="data[@alias = 'colourCode']" />
                        </xsl:apply-templates>                    
                    </xsl:otherwise>
                </xsl:choose>
            </ol>
        </xsl:template>
    
        <xsl:template match="node">
            <li>
                <strong><xsl:value-of select="@nodeName" /></strong> (<xsl:value-of select="data[@alias = 'colourCode']" />)
            </li>
        </xsl:template>
    
    
        <xsl:template match="node[data[@alias = 'umbracoNaviHide'] = '1']">
            <!-- No output for these items -->
        </xsl:template>
    

    /Chriztian

     

  • Chad 65 posts 129 karma points c-trib
    Oct 16, 2009 @ 03:09
    Chad
    0

    Thanks for everyones help. Finally got it all working!

Please Sign in or register to post replies

Write your reply to:

Draft