开发者

Limit sorted conditional xsl:for-each results to n values

I looked around on the site but wasn't able to locate a solution to a particular XSL problem. If you recognize this as a duplicate post I apologize and would be thankful to be pointed in the right direction.

Based on the relatively simple XML data set below I want to create a table by project but also limit the entries to something more 'digestible' like the 'newest' 10 for that project.

+---------------------------------------------------+  
| Alpha                   | Beta                    |  
+---------------------------------------------------+  
| Log_20091014_0900PM.xml | Log_20091015_0900PM.xml |  
| Log_20091013_0900PM.xml | ...                     |  
| ...                     |                         |  
+---------------------------------------------------+  
| Gamma                   | Delta                   |  
+---------------------------------------------------+  
| ...                     | ...                     |  
+---------------------------------------------------+  

XML source data of the form:

<LogResults>
  <Result>
    <Project>Alpha</Project>
    <Data>Log_20091013_0900PM.xml</Data>
    <Name>Log_20091013_0900PM.xml</Name>
  </Result>
  <Result>
    <Project>Alpha</Project>
    <Data>Log_20091014_0900PM.xml</Data>
    <Name>Log_20091014_0900PM.xml</Name>
  </Result>
  <Result>
    <Project>Beta</Project>
    <Data>Log_20091015_0900PM.xml</Data>
    <Name>Log_20091015_0900PM.xml</Name>
  </Result>
  <Result>
    <Project>Gamma</Project>
    <Data>Log_20091016_0900PM.xml</Data>
    <Name>Log_20091016_0900PM.xml</Name>
  </Result>
  <Result>
    <Project>Delta</Project>
    <Data>Log_20091019_0900PM.xml</Data>
    <Name>Log_20091019_0900PM.xml</Name>
  </Result>
  <Result>
    <Project>Delta</Project>
    <Data>Log_20091020_0900PM.xml</Data>
    <Name>Log_20091020_0900PM.xml</Name>
  </Result>
  ...
</LogResults>

I am able to show ALL results for each project with appropriate variations of the following XSL:

<xsl:for-each select="LogResults/Result">
  <xsl:sort select="Data" order="descending" />
  <xsl:if test="(Project='Alpha')">
    <li>
      <a style="font-size:11pt;">
        <xsl:attribute name="href">
          Alpha/<xsl:value-of select="Data" />
        </xsl:attribute>
        <xsl:value-of select="Name" />
      </a>
    </li>
  </xsl:if>
</xsl:for-each>

Question:

Is it possible to limit the results to the 'newest' 10 (or 20 ...)? And if so how would you suggest this to be done?

I tried to use position() in the following way for example

<xsl:for-each select="(LogResults/Result) [position &lt; 11]">

or

<xsl:if test="(Project='Alpha')">
  <xsl:for-each select=". [position &lt; 11]">
    <li>
      <a style="font-size:11pt;">
        <xsl:attribute name="href">
         开发者_如何学C Alpha/<xsl:value-of select="Data" />
        </xsl:attribute>
        <xsl:value-of select="Name" />
      </a>
    </li>
  </xsl:for-each>
</xsl:if>

But so far I couldn't get it to work. Certainly because it's more trail and error right now.

Thanks a lot for even reading this and any potential advice. -T


You are quite close to the correct solution.

Use:

<xsl:for-each select="LogResults/Result[Project='Alpha']">
  <xsl:sort select="Data" order="descending" />
  <xsl:if test="not(position() > 10)">
    <li>
      <a style="font-size:11pt;">
        <xsl:attribute name="href">
          Alpha/<xsl:value-of select="Data" />
        </xsl:attribute>
        <xsl:value-of select="Name" />
      </a>
    </li>
  </xsl:if>
</xsl:for-each>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜