Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 24th, 2011, 11:46 PM
Authorized User
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default post increment and arrays situation in xslt


i have a requirement where i have to output a xml with <msg> nodes not more than 5.Each value of the <msg> node is based on different conditional logic using different variable values.My problem is how do i exit from the template as soon as the xml exceeds 5 <msg> nodes or continue ahead if the msg nodes cnt doesnt exceed 5.each msg node value is determined by different conditions using different variable values.

<xsl:template match="/">
<xsl:variable name="msgop">

<xsl:variable name="msgcnt" select="count(a/msg)"/>

<xsl:variable name="msgdsp">
<xsl:call-template name="recur">
<xsl:with-param name="msgcnt"/>


<xsl:template name="recur">
<xsl:param name="msgcnt"/>
<xsl:if test $msgcnt < 5>
execute with some variable value (each msg will use different variable to retrieve content)
<xsl:call-template name="recur">
<xsl:wih-param name="msgcnt" select="$msgcnt+1"/>

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old July 25th, 2011, 04:37 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts

How about something like this:

<xsl:copy-of select="$msgop/a/msg[position() &lt; 5]"/>
That will output the first 5 <msg> elements in the variable you have created above.

If you want to just use these first 5 <msg> elements to do some other processing then call <xsl:apply-templates> instead of <xsl:copy-of>.

To comment on your example XSLT specifically - your recursive template would a) never exit as it always calls itself again (use xsl:choose, xsl:when and xsl:otherwise to do this). b) you don't need the 'global' variable msgcnt, c) You would need to pass 0 in to the first call to xsl:with-param, and d) you've obviously not tried to run this as there are obvious spelling mistakes.
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hints for a good XSLT post joefawcett XSLT 4 October 30th, 2017 03:23 AM
Increment tag by 1 using XSLT Divya XSLT 10 April 13th, 2010 05:39 AM
Arrays in xslt rabs XSLT 9 June 19th, 2009 10:32 AM
Getting post/get parameters with XSLT? snorripall XSLT 2 October 6th, 2006 11:39 AM
strange situation Vishal_7 ASP.NET 1.0 and 1.1 Basics 0 December 16th, 2004 07:18 PM

All times are GMT -4. The time now is 08:28 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.