XSLT nested sort / for-each

懵懂的女人 提交于 2019-12-13 14:12:29

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/3816109/xslt-nested-sort-for-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!