<xsl:for-the-first-three-or-so select="item">?

D

Drulli Rokk

Hi,

Here's a question that has cost this newbie two days of headache already:
How can I get my XSLT stylesheet to specify a maximum number of elements to
process?

I'm now using <xsl:for-each> which processes all the child-nodes at the
particuar point in my XML (RSS) file. But what I really want, is this:

if number of child-nodes > n then process only the first n
else process all of them.

If anyone can point me to the general direction in which to look, or what
words to enter into google, I will be much obliged.


The part of the XML in question looks like this

<rss>
<channel>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
 
M

Mike Brown

Drulli Rokk said:
if number of child-nodes > n then process only the first n
else process all of them.
[...]
The part of the XML in question looks like this

<rss>
<channel>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
.
.
.
<item>...</item>
</channel>
</rss>

and the XSLT currently handling them contains this:
<xsl:template match="rss/channel">

No need for the "rss/" here, unless you have channel elements that are
children of other nodes. A match pattern is not identifying nodes to go
process; it is identifying which nodes, if they happen to be selected for
processing, should be processed with this template.
<xsl:for-each select="item">
...
</xsl:for-each>
</xsl:template>

Just change the select in the xsl:for-each from "item" to "item[position()
&lt; 10]", if you want to process the first 9 (if there are less than 9,
it'll process all of them)
 
D

Drulli Rokk

Thank you. Not only does this work, I understand it as well!

Mike Brown said:
Just change the select in the xsl:for-each from "item" to
"item[position()
&lt; 10]", if you want to process the first 9 (if there are less than 9,
it'll process all of them)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top