By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,454 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,454 IT Pros & Developers. It's quick & easy.

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

P: n/a
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>
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Drulli Rokk" <ig********@spamalready.com> wrote
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)
Jul 20 '05 #2

P: n/a
Thank you. Not only does this work, I understand it as well!

Mike Brown <mi**@REMOVETHIS.skew.org> wrote:

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)


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 20 '05 #3

P: n/a
Drulli Rokk <ig********@spamalready.com> wrote:
<xsl:for-each select="item">
...
</xsl:for-each>


<xsl:for-each select="item[position() &lt;= 3]">
...
</xsl:for-each>
--
David
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.