Reputation: 4284
I have an XML with let's say 100 elements item
.
Now I want to make a loop over the first 10 items with a certain condition.
The conditional for loop could be like this:
<xsl:for-each select="//z:item[@promoted='true' and @prio='true']">
...
</xsl:for-each>
How do I process the first 10 elements only?
An easy solution would be this:
<xsl:for-each select="//z:item[@promoted='true' and @prio='true']">
<xsl:if test="position < 11">
...
</xsl:if>
</xsl:for-each>
However this has the disadvantage, that the loop runs for all 10'000 items. How can I make the for loop to include the condition "only the first 10" so that the loop really only goes over the first 10 items?
Upvotes: 0
Views: 314
Reputation: 1882
The first 10 that meet the condition:
//z:item[@promoted='true' and @prio='true'][11 > position()]
From the first 10 only those that meet the condition
//z:item[11 > position()][@promoted='true' and @prio='true']
Upvotes: 1
Reputation: 11986
You can make it part of your XPATH predicate, for example:
<xsl:for-each select="//z:item[@promoted='true' and @prio='true'][position() < 11]">
Upvotes: 1