Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
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 Search this Thread Display Modes
  #1 (permalink)  
Old November 26th, 2008, 09:36 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default Recursion problem

Hi!

I have to do this transformation:

Source:
Code:
<root>

    <Invoice>
        <Invoice_Num>0</Invoice_Num>
        <InvoiceData1>aa</InvoiceData1>
        <InvoiceData2>bb</InvoiceData2>
    </Invoice>

    <Delivery>
        <Invoice_Num></Invoice_Num>
        <Delivery_Num>1</Delivery_Num>
        <DelData1>aa</DelData1>
        <DelData2>dd</DelData2>
    </Delivery>
    <Line>
        <Delivery_Num></Delivery_Num>
        <LineData1>1234</LineData1>
        <LineData2>4321</LineData2>
    </Line>
 
</root>
Target:

Code:
<root>

    <Invoice>
        <Invoice_Num>0</Invoice_Num>
        <InvoiceData1>11</InvoiceData1>
        <InvoiceData2>22</InvoiceData2>
    </Invoice>

    <Delivery>
        <Invoice_Num>0</Invoice_Num>
        <Delivery_Num>1</Delivery_Num>
        <DelData1>aa</DelData1>
        <DelData2>dd</DelData2>
    </Delivery>
    <Line>
        <Delivery_Num>1</Delivery_Num>
        <LineData1>1234</LineData1>
        <LineData2>4321</LineData2>
    </Line>
 
</root>
As you can see each "Delivery" must get the number of previous "Invoice", and each "Line" the number of previous "Delivery".
Each of nodes occures from 0-n times (except "Invoice", it's always occurs at least one time).
As I need to use XSLT 1.0(I' am building the mapping for SAP XI) the only way to do this kind of transformation is using the recursion, am I right?
Actually I can't understand the logic how the recursive function must work in this case, what elements I must pass (Invoice? Delivery? Line?)
My be I need three different recursive functions for each type of node? Any help would be greatly welcomed!
Reply With Quote
  #2 (permalink)  
Old November 26th, 2008, 09:47 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't see why you need recursion. Accessing the preceding-sibling of the parent should suffice:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">


  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Delivery/Invoice_Num">
    <xsl:copy>
      <xsl:value-of select="../preceding-sibling::Invoice[1]/Invoice_Num"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Line/Delivery_Num">
    <xsl:copy>
      <xsl:value-of select="../preceding-sibling::Delivery[1]/Delivery_Num"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
--
  Martin Honnen
  Microsoft MVP - XML
Reply With Quote
  #3 (permalink)  
Old November 26th, 2008, 09:49 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
Default

For the Deliveryelement:

<xsl:template match="Delivery">
<Delivery>
   <Invoice_Num><xsl:value-of select="preceding-sibling::Invoice[1]/Invoice_Num"/></Invoice_Num>
   <xsl:apply-templates/>
</Delivery>
</xsl:template>
<xsl:template match="Delivery/Invoice_num" />

Repeat for Line and add an Identity transform to complete the stylesheet.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #4 (permalink)  
Old November 26th, 2008, 09:59 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you Martin, you are right. It works without recursion. Well I learned something new.
Thank's again!

Reply With Quote
  #5 (permalink)  
Old November 26th, 2008, 10:00 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 For the Deliveryelement:

<xsl:template match="Delivery">
<Delivery>
<Invoice_Num><xsl:value-of select="preceding-sibling::Invoice[1]/Invoice_Num"/></Invoice_Num>
<xsl:apply-templates/>
</Delivery>
</xsl:template>
<xsl:template match="Delivery/Invoice_num" />

Repeat for Line and add an Identity transform to complete the stylesheet.

/- Sam Judson : Wrox Technical Editor -/
Thank you Sam!

Reply With Quote
  #6 (permalink)  
Old November 26th, 2008, 10:05 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
Default

Yeah, Martin's solution is better.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
Reply


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
xsl recursion problem vinayreddy XSLT 5 August 14th, 2008 07:14 AM
Recursion lincsimp XSLT 1 August 16th, 2005 04:26 PM
recursion yui0329 C# 6 April 28th, 2005 09:36 AM
Recursion shan9 JSP Basics 0 November 17th, 2004 09:29 PM
recursion nulogix PHP How-To 1 June 28th, 2004 03:58 PM



All times are GMT -4. The time now is 07:17 PM.


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