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 November 26th, 2008, 09:36 AM
Authorized User
 
Join Date: Sep 2008
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!
 
Old November 26th, 2008, 09:47 AM
Friend of Wrox
 
Join Date: Nov 2007
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
 
Old November 26th, 2008, 09:49 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old November 26th, 2008, 09:59 AM
Authorized User
 
Join Date: Sep 2008
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!

 
Old November 26th, 2008, 10:00 AM
Authorized User
 
Join Date: Sep 2008
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!

 
Old November 26th, 2008, 10:05 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yeah, Martin's solution is better.

/- Sam Judson : Wrox Technical Editor -/





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





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