开发者

XSLT: for each limit acts strange

I have a for each with a limit of 3 items to display. The problem is that it if I limit it to 3...it doesn't begin with the first entry as it supposed to do.开发者_开发问答 In stead it starts at the second one. I have my entries ranked from 001 to 020. And I sorted the for-each on this rank. So the first 4 would be displayed. So there should be something wrong with my for-each statements?

<xsl:for-each select="data/activity-feed/entry[recommended = 'Yes'][position() mod 4]">
   <xsl:sort select="activity-rank" data-type="number" order="ascending" />  
        <a href="{$root}/items">
                <p>  
                     <xsl:value-of select="activity-name"/>
                     <xsl:call-template name="description">
                </p>
        </a>
 </xsl:for-each>

My XML:

<data>
    <activity-feed>
        <entry id="65">
                <recommended>Yes</recommended>
                <activity-name handle="forra-dive">Forra Dive</activity-name>
                <activity-rank handle="001">001</activity-rank>
        </entry>
    </activity-feed>
</data>

Any idea how to make only the FIRST 4 display? Thanks


I think the problem is that in the for-each select, position() is evaluated in the unsorted order of the entry. So you're getting the first 3 in the original (document) order instead of in the ranked order. If you show more of your input data we could tell for sure.

If that's the case, try:

<xsl:for-each select="data/activity-feed/entry[recommended = 'Yes']">
   <xsl:sort select="activity-rank" data-type="number" order="ascending" />  
   <xsl:if test="position() &lt; 4">
      <a href="{$root}/items">
         <p>  
            <xsl:value-of select="activity-name"/>
            <xsl:call-template name="description">
         </p>
      </a>
   </xsl:if>
 </xsl:for-each>

The fix is that we're testing for position() inside the sorted for-each loop, instead of "outside" the sort.


Why do you use modulus? If you use a static number, the loop limitation should succeed.

<xsl:for-each select="data/activity/entry[recommended='Yes'][position() < 4]>
    ...
</xsl:for-each>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜