468,133 Members | 1,123 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

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

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
3 4390
"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
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
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.

By using this site, you agree to our Privacy Policy and Terms of Use.