basZero
basZero

Reputation: 4284

How do you loop over the first 10 elements with a certain condition in XSLT?

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 &lt; 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

Answers (2)

Alejandro
Alejandro

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

user268396
user268396

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() &lt; 11]">

Upvotes: 1

Related Questions