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 October 29th, 2008, 02:23 AM
Authorized User
 
Join Date: Sep 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default Convert Flat structure to Nested

Hi! I need do convert a flat structure to nested one.
I know how to do this using the condition, but now I don't have any common "keys" between the items.
I need to save the original order of the items.

[u]Source structure:</u>

<root>
    <Invoice>
        <InvType>0</InvType>
        <InvNum>111</InvNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>111</DeliveryNum>
        <Amount>100.00</Amount>
    </Delivery>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>333</DeliveryNum>
        <Amount>200.00</Amount>
    </Delivery>
    <Invoice>
        <InvType>0</InvType>
        <DeliveryNum>222</DeliveryNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>222</DeliveryNum>
        <Amount>300.00</Amount>
    </Delivery>
</root>

[u]Target:</u>

<root>
    <Invoice>
        <InvType>0</InvType>
        <InvNum>111</InvNum>
        <Delivery>
             <DeliveryType>1</DeliveryType>
             <DeliveryNum>111</DeliveryNum>
             <Amount>100.00</Amount>
        </Delivery>
        <Delivery>
             <DeliveryType>1</DeliveryType>
             <DeliveryNum>333</DeliveryNum>
             <Amount>200.00</Amount>
        </Delivery>
    </Invoice>

    <Invoice>
        <InvType>0</InvType>
        <DeliveryNum>222</DeliveryNum>
        <Delivery>
                <DeliveryType>1</DeliveryType>
                <DeliveryNum>222</DeliveryNum>
                <Amount>300.00</Amount>
        </Delivery>
    </Invoice>
</root>

[u]Here is my code:</u>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <root>
            <xsl:for-each select="root/Invoice">
                <Invoice>
                    <InvType>
                        <xsl:value-of select="InvType"/>
                    </InvType>
                    <InvNum>
                        <xsl:value-of select="InvNum"/>
                    </InvNum>
********Here I can insert a condition, but I don't have any :(*******
For example: <xsl:for-each select="../Delivery[DeliveryType=../Invoice/InvType]">
************************************************** *****************
                        <Delivery>
                            <DeliveryType>
                                <xsl:value-of select="DeliveryType"/>
                            </DeliveryType>
                            <DeliveryNum>
                                <xsl:value-of select="DeliveryNum"/>
                            </DeliveryNum>
                            <Amount>
                                <xsl:value-of select="Amount"/>
                            </Amount>
                        </Delivery>
                    </xsl:for-each>
                </Invoice>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:stylesheet>

How can solve the problem?
 
Old October 29th, 2008, 02:36 AM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi,

<Invoice>
        <InvType>0</InvType>
        <InvNum>111</InvNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>111</DeliveryNum>
        <Amount>100.00</Amount>
    </Delivery>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>333</DeliveryNum>
        <Amount>200.00</Amount>
    </Delivery>
    <Invoice>
        <InvType>0</InvType>
        <DeliveryNum>222</DeliveryNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>222</DeliveryNum>
        <Amount>300.00</Amount>
    </Delivery>

is this the way u r expecting

<xsl:template match="/">
        <xsl:copy-of select="./node()" />
    </xsl:template>

will give the result as i mentioned.here the invoice tag is ending before the delivery tag.


Raj
 
Old October 29th, 2008, 02:51 AM
Authorized User
 
Join Date: Sep 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by rajesh_css
 Hi,

<Invoice>
        <InvType>0</InvType>
        <InvNum>111</InvNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>111</DeliveryNum>
        <Amount>100.00</Amount>
    </Delivery>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>333</DeliveryNum>
        <Amount>200.00</Amount>
    </Delivery>
    <Invoice>
        <InvType>0</InvType>
        <DeliveryNum>222</DeliveryNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>222</DeliveryNum>
        <Amount>300.00</Amount>
    </Delivery>

is this the way u r expecting

<xsl:template match="/">
        <xsl:copy-of select="./node()" />
    </xsl:template>

will give the result as i mentioned.here the invoice tag is ending before the delivery tag.
Raj
Hi Raj!

This is the source flat structure:

<Invoice>
        <InvType>0</InvType>
        <InvNum>111</InvNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>111</DeliveryNum>
        <Amount>100.00</Amount>
    </Delivery>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>333</DeliveryNum>
        <Amount>200.00</Amount>
    </Delivery>
    <Invoice>
        <InvType>0</InvType>
        <DeliveryNum>222</DeliveryNum>
    </Invoice>
    <Delivery>
        <DeliveryType>1</DeliveryType>
        <DeliveryNum>222</DeliveryNum>
        <Amount>300.00</Amount>
    </Delivery>

I expecting to the nested structure, like that:

<root>
    <Invoice>
        <InvType>0</InvType>
        <InvNum>111</InvNum>
        <Delivery>
             <DeliveryType>1</DeliveryType>
             <DeliveryNum>111</DeliveryNum>
             <Amount>100.00</Amount>
        </Delivery>
        <Delivery>
             <DeliveryType>1</DeliveryType>
             <DeliveryNum>333</DeliveryNum>
             <Amount>200.00</Amount>
        </Delivery>
    </Invoice>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Flat XML to nested IgorK XSLT 10 November 24th, 2008 09:56 AM
Convert nested XML to flat one dani1 XSLT 15 October 21st, 2008 02:20 AM
Flat DB structure into XML Hierarchy DaveQuested XSLT 1 January 29th, 2007 10:25 AM
Flat XML structure to a tree by XSLT Borg0011 XSLT 2 July 9th, 2006 08:58 AM
mapping repeating xml structure to flat csv f vasmallela Biztalk 0 May 29th, 2005 11:53 PM





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