473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

<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 4585
"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 thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.