开发者

XSLT: for each problem

开发者 https://www.devze.com 2023-01-21 23:58 出处:网络
It supposed to be very simple, I want to limit my results in this for-each but also want to check a node.

It supposed to be very simple, I want to limit my results in this for-each but also want to check a node. For some reason it doesn't work combining them both in one statement?

I have a checkbox (recommended) which can be ticked to 'Yes'. When I remove the limit开发者_Go百科 5 in the for-each, it shows all the items that have recommended on 'Yes'. When I remove the 'recommended = 'Yes'' from the for-each test, I got the last items limited till 5. Putting them together results in nothing. They both don't work anymore. I want to filter them on Recommended 'Yes' and I want to limit them to 5.

<xsl:for-each select="data/resorts/entry[position() &lt; 2 and recommended = 'Yes']">
   <xsl:sort select="top-pick-order" case-order="upper-first"/>
       <a href="{$root}/koh-lipe-resorts/resort-view/{resort-name/@handle}">
           <div id="top-pick-item">
                 <div id="top-pick-text-short">
                  <h3 class="item-heading"><xsl:value-of select="resort-name"/></h3>
                  <p>   
                      <xsl:call-template name="truncate">
                          <xsl:with-param name="value" select="resort-description" mode="formatted"/>
                          <xsl:with-param name="length" select="110" />
                      </xsl:call-template>
                  </p>
                 </div>
           </div>
         </a>
</xsl:for-each>


<xsl:for-each select=
   "data/resorts/entry[position() &lt; 6
  and
    recommended = 'Yes']">

This will select all the data/resorts/entry elements among the first two, that also have a recommended attribute with value 'Yes'.

But you want:

data/resorts/entry[recommended = 'Yes'][position() &lt; 6]

this expression first specifies all data/resorts/entry elements and then limits them only to those whose position in the already obtained node-list is less than 6.

Remember: The position() function is context-sensitive and it cannot be freely moved around an expression without changing its meaning!


Since you did not provide some sample data yet I can only assume that recommended is an attribute of <entry />. In that case you need to have @recommended in your select statement:

<xsl:for-each select="data/resorts/entry[position() &lt; 2 and @recommended = 'Yes']">
0

精彩评论

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