xsl-list
[Top] [All Lists]

Re: need to limit a For:each loop

2005-05-05 08:44:58
the current output can be seen here
http://www.discountfirepagers.com/rss2/default.asp
as you can see there are many articles ...

Word of advice, I know that some people on the list don't like to
follow links out for information for several reasons: It's a potential
security issue, it usually means a huge input/output (doesn't seem to
be the case this time), and it's just another step.  Usually you get
better mileage with smaller examples in the body of the email..


I wish to limit the articles to just 5 items. however i can't figure how
to modify the XSL to do that for me
i know i have to break the for:each somehow or change it to a specified
loop but can't find any info on how to do this

Because there isn't really any "break".  XSLT is a descriptive
language, much more akin to SQL.

If you just want the first five, say so. 

<!-- you really should be climbing down the tree, not using //*.   -->
<!-- lets create a node-group of just the items -->

<xsl:variable name="items" select="//*[local-name()='item']"/>

<!-- now for ever node in item that has a position less than six do ... -->

<xsl:for-each select="$items[position() < 6]">

whatever you want to do in the loop

  </xsl:for-each>

You could probably be happy and stop there.  But I still don't think
it's quite a spiffy as it could be.   Sorry I don't have the time now
to rework it (need to work on my own rss script).

Also, one last comment.  The templates you have seem a little poorly
chosen.  Without looking at it too much here's what happens.

You get to the root element.  Most specific template matching is with
match="/".  Now you apply templates to all the children (in this case
the element node "rss").

Only one template matches: "*".  No templates are called or named in
this template so processing stops.

you might as well move all your code into the match="/".  If you get
into the habit of using that type of set up (matching="*" and using
"//*" inside the template) you'll get burned at some point and start
getting duplicates.

Some suggestions:

Use more templates and apply templates.

Check out namespaces and how to use them.  local-name() might be
necessary but most likely not.

I'd advise using a command-line xslt processor like Saxon.  I'm
willing to bet you are using a browser to view your results right?  I
find Saxon and the command line much , much nicer for debugging.

Dave  Pawson's FAQ for this list is excellent     
http://www.dpawson.co.uk/xsl/index.html.

Also, the archives tend to have answers to most basic questions.

I'm willing to bet there are plenty of materials and examples out
there on just transforming RSS with XSLT.


Sorry I don't have more time to give examples.  Try creating scripts
that crawl up and down the rss just for practice, or create recursive
functions.  Those seem good ways for getting into some of the more
complex issues.

Jon Gorman




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>