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 January 8th, 2010, 06:36 PM
Authorized User
 
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default decrease recursive depth

Hi ,
Can anyone help to improvise this code to reduce recursion depth.It is giving stack overflow if the input-string lenght.greter than 10000.the below template is for removing trailing spaces.

Thanks



HTML Code:
<xsl:template match="@*|node()">
- <xsl:copy>
  <xsl:apply-templates select="@*|node()" /> 
  </xsl:copy>
  </xsl:template>
- <xsl:template match="*">
- <xsl:copy>
  <xsl:apply-templates select="@*" /> 
- <xsl:choose>
- <xsl:when test="not(*)">
- <xsl:variable name="new-value">
- <xsl:call-template name="remove-trailing-char">
  <xsl:with-param name="input-string" select="." /> 
  <xsl:with-param name="word" select="' '" /> 
  </xsl:call-template>
  </xsl:variable>
  <xsl:value-of select="$new-value" /> 
  </xsl:when>
- <xsl:otherwise>
  <xsl:apply-templates select="*" /> 
  </xsl:otherwise>
  </xsl:choose>
  </xsl:copy>
  </xsl:template>
- <xsl:template name="remove-trailing-char">
  <xsl:param name="input-string" /> 
  <xsl:param name="word" /> 
  <xsl:variable name="last-char" select="substring($input-string, string-length($input-string), 1)" /> 
- <xsl:choose>
- <xsl:when test="not($last-char='') and contains($word, $last-char)">
- <xsl:call-template name="remove-trailing-char">
  <xsl:with-param name="input-string" select="substring($input-string, 1, string-length($input-string)-1)" /> 
  <xsl:with-param name="word" select="$word" /> 
  </xsl:call-template>
  </xsl:when>
- <xsl:otherwise>
  <xsl:value-of select="$input-string" /> 
  </xsl:otherwise>
  </xsl:choose>
  </xsl:template>
 
Old January 8th, 2010, 08:36 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, in an ideal world you could use an XSLT 2.0 processor, or failing that, an XSLT 1.0 processor that implements tail recursion. But if you can't do either of those things then (assuming you only want to solve the specific problem of removing trailing spaces, rather than the more general functionality of your remove-trailing-char template) you could try a divide-and-conquer approach like this:

Find the mid-point of the string. If the rhs is all spaces, make a recursive call to process the lhs. Otherwise, copy the lhs and make a recursive call to process the rhs. The test whether a string is all spaces is not(normalize-space($X)).

This gives you a max recursive depth of log(N) where N is the string length.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 8th, 2010, 11:04 PM
Authorized User
 
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Excellent thanks Kay,was able to resolve it using the divide and conquer approach





Similar Threads
Thread Thread Starter Forum Replies Last Post
Decrease newsletters nabeelalkaff BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 December 14th, 2007 11:29 PM
How to Decrease the log file (.ldf) size mike.chary SQL Server 2005 4 September 7th, 2007 07:31 PM
Image Color depth and mode qazi_nomi ASP.NET 1.0 and 1.1 Professional 3 January 24th, 2007 02:49 PM
text increase/decrease ... jstewie Javascript How-To 0 July 12th, 2005 12:13 PM
Depth First Search gbilios C++ Programming 3 June 8th, 2004 12:28 PM





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