views:

42

answers:

1

I am trying to figure out the best way to perform a nested for-each & sort. For example my XSLT below performs the following:

  1. Selects a node set.

  2. Sorts by Date attribute.

  3. Performs position() to pull 4 nodes.

Now, the next part is what I am trying to figure out. After step 3 I need to sort by the Date attribute again, but this time in asceding order...now that the data set is narrowed down to 4 nodes. Suggestions? Thanks for the help!!


XSLT:

  <xsl:template name="AdmissionsNewsList">
    <ul class="stories-list">
      <xsl:for-each select="AdmissionsSectionFront/TaxonomyNavigation[@Name='AdmissionsNewsList']/Category/Pages/Page">
        <xsl:sort select="@Date" order="descending" />
              <xsl:if test="position() &lt; 5">

                    <!-- NOW THAT THE NODES HAVE BEEN SELECTED, SORTED, AND POSTION LIMIT SET
                         I NEED TO PERFORM ANOTHER SORT BY DATE (ASCENDING ORDER) HERE ON THE NARROWED DOWN DATA SET -->

                            <li>
                              <xsl:choose>
                                  <xsl:when test="normalize-space(@ThumbnailImage)">
                                      <img alt="{@LocalAlternateText}" src="images/{@ThumbnailImage}" />
                                  </xsl:when>
                                  <xsl:otherwise>
                                      <img alt="{@LocalAlternateText}" src="images/News%20Photos/Default.jpg"/>
                                  </xsl:otherwise>
                              </xsl:choose>
                              <div>
                                <h5>
                                  <xsl:value-of select="@Title"/>
                                </h5>
                                <br/>
                                <xsl:apply-templates select="@Abstract"/>
                                <br/><br/>
                                <a href="{@URL}" class="full-story">
                                  <xsl:apply-templates select="Page[@Name='Link']/@Target"/>Full Story
                                </a>
                              </div>
                            </li>

              </xsl:if> 
      </xsl:for-each>
    </ul>
  </xsl:template>

NOTE: This had to be in XSLT 1.0 & is using MSXML 4.0 Parser. Thanks in advance!

+2  A: 

Instead of:

  <xsl:for-each select=
    "AdmissionsSectionFront/TaxonomyNavigation
                    [@Name='AdmissionsNewsList']/Category/Pages/Page">
    <xsl:sort select="@Date" order="descending" />
          <xsl:if test="position() &lt; 5">

use:

  <xsl:variable name="vNewAdmPages" select=
    "AdmissionsSectionFront/TaxonomyNavigation
                    [@Name='AdmissionsNewsList']/Category/Pages/Page">
  "/>
  <xsl:for-each select="$vNewAdmPages">
    <xsl:sort select="@Date" order="ascending" />
          <xsl:if test="position() > count($vNewAdmPages) -5">

In this way you need a single sort and get directly to the five nodes of interest.

Dimitre Novatchev
@Dimitre: +1 Very good answer! First four in descending order should be last four in ascending order.
Alejandro
src0010