Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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

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
 
Old July 25th, 2011, 04:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
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?





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





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