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 March 20th, 2007, 08:09 AM
Tre Tre is offline
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Recursive template - Please Help!

Hi Guys,

I am new to this forum and to XSLT but I was hoping that maybe someone may be able to tell me what I am doing wrong in my recursive template.

I am using recursion to try to increment a parameter but for some reason the parameter never gets incremented when i use this for-each loop setup. To be honest I don't think the recursive call is working at all, it simply shows the page break text and skips the recursive call from what I can make out.

If you look at a sample of my xml document below I want to loop through all the "Security" nodes and process all the nodes with the same "PortfolioID". When I hit a different PortfolioID in the for-each loop I want to add a page break, increment the parameter and recursively call the template again to process all the "Securities" with the next "PortfolioID".

Below is the template in question:

  <xsl:template name="runBody">
    <xsl:param name="currentPortfolio"/>
    <xsl:for-each select="NewDataSet/Security">
    <xsl:if test="number(.//PortfolioID)!=number($currentPortfolio)">

        ########### I WILL PUT A PAGE BREAK HERE ###################

        <xsl:call-template name="runBody">
           <xsl:with-param name="currentPortfolio"select="$currentPortfolio + 1"/>
        </xsl:call-template>
      </xsl:if>
      <xsl:call-template name="MainBody"/>
    </xsl:for-each>
  </xsl:template>


A small sample of my .xml document is as follows:

- <NewDataSet>
- <Security>
  <PortfolioID>1</PortfolioID>
  <Quantity>183.84</Quantity>
  <FirstName>James</FirstName>
  <LastName>Carter</LastName>
  <Description>Stone Money Market</Description>
  <SecurityID>20</SecurityID>
  </Security>
- <Security>
  <PortfolioID>1</PortfolioID>
  <Quantity>623.08</Quantity>
  <FirstName>James</FirstName>
  <LastName>Carter</LastName>
  <Description>Vantage Bond Index Fund</Description>
  <SecurityID>24</SecurityID>
  </Security>
- <Security>
  <PortfolioID>1</PortfolioID>
  <Quantity>605.358</Quantity>
  <FirstName>James</FirstName>
  <LastName>Carter</LastName>
  <Description>Pimco Total Return Fund</Description>
  <SecurityID>25</SecurityID>
  </Security>
- <Security>
  <PortfolioID>1</PortfolioID>
  <Quantity>454.295</Quantity>
  <FirstName>James</FirstName>
  <LastName>Carter</LastName>
  <Description>Montgomery Equity Income</Description>
  <SecurityID>26</SecurityID>
  </Security>
- <Security>
  <PortfolioID>2</PortfolioID>
  <Quantity>1147.6</Quantity>
  <FirstName>Kathleen</FirstName>
  <LastName>Carter</LastName>
  <Description>Stone Money Market</Description>
  <SecurityID>20</SecurityID>
  </Security>
- <Security>
  <PortfolioID>2</PortfolioID>
  <Quantity>706.667</Quantity>
  <FirstName>Kathleen</FirstName>
  <LastName>Carter</LastName>
  <Description>Pimco Total Return Fund</Description>
  <SecurityID>25</SecurityID>
  </Security>


Thanks in advance for any advice, comments or help you can give me.

Kind regards,

Trevor.
 
Old March 20th, 2007, 09:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

When you do

<xsl:for-each select="NewDataSet/Security">

you change the context node to be a Security element. So in your recursive call, "." is a Security element, and you then select <xsl:for-each select="NewDataSet/Security">. As far as I can see no Security element in your data has a child called NewDataSet.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 20th, 2007, 09:14 AM
Tre Tre is offline
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So how would I go about fixing the problem?

 
Old March 20th, 2007, 09:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't actually understand why you need recursion here at all. Just process all the records in a normal loop and output the page break if PortfolioId != preceding-sibling::*[1]/PortfolioId. But perhaps I haven't understood your problem

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 20th, 2007, 10:08 AM
Tre Tre is offline
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael that worked perfectly! I was obviously barking up the wrong tree with recursion.

I don't suppose you know an easy way of adding a page break do you? I have been looking through your book which I have here but can't seem to find anything in there or on the net.

 
Old March 20th, 2007, 11:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I don't suppose you know an easy way of adding a page break do you?

I don't know what your target vocabulary is. It it's HTML, I don't think HTML has any concept of pages or page breaks. If it's XSL-FO, or RTF, or OpenOffice, or something else, then you need to ask someone who understands that vocabulary.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in Recursive applying template ismomo XSLT 3 December 29th, 2007 11:35 PM
Recursive Template Help bucky483 XSLT 1 July 20th, 2007 12:21 PM
transform in a loop vs recursive template ramarc XSLT 3 April 10th, 2006 04:40 PM
Using a recursive template for various nodes spencer.clark XSLT 5 August 2nd, 2005 03:49 PM
Recursive template to split up string and extract alexshiell XSLT 2 December 16th, 2004 09:04 AM





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