Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 25th, 2011, 12:46 AM
Authorized User
 
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default post increment and arrays situation in xslt

Hi,

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">
<a>
<msg>content..</msg>
<msg>contet.....</msg>
</a>
</xsl:variable>

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

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

</xsl:template>

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

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old July 25th, 2011, 05: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,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

How about something like this:

Code:
<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
Reply


Thread Tools
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 04:23 AM
Increment tag by 1 using XSLT Divya XSLT 10 April 13th, 2010 06:39 AM
Arrays in xslt rabs XSLT 9 June 19th, 2009 11:32 AM
Getting post/get parameters with XSLT? snorripall XSLT 2 October 6th, 2006 12:39 PM
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 10:46 AM.


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