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 23rd, 2008, 10:35 AM
Registered User
 
Join Date: Nov 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Flat XML to nested

Hi!

I need to convert some flat XML to nested one. But I don't know if it's possible.

Can anyone help?


Here is my source structure:

Code:
<root>
    <Level_1>
        <Item_1></Item_1>
        <Item_2></Item_2>
    </Level_1>

    <Level_2>
        <Item_21></Item_21>
        <Item_22></Item_22>
    </Level_2>

    <Level_3>
        <Item_31></Item_31>
        <Item_32></Item_32>
    </Level_3>
</root>
This is the target:

Code:
<root>
 <Level_1>
        <Item_1></Item_1>
        <Item_2></Item_2>
     <Level_2>
          <Item_a></Item_a>
          <Item_b></Item_b>
       <Level_3>
           <Item_a1></Item_a1>
           <Item_a2></Item_a2>
       </Level_3>
     </Level_2>
 </Level_1>
</root>
Each of levels can have 1 to n occurrences.

I found a post with similar problem:
(as I am a new member I can't include URL in my post, but you can see it on page 5, post named "Group by" problem )
But there was some relation between nodes and I don't have any.

So I don't know if it's possible, and I don't have any idea how to do this :(

Thanks for any help.
Igor
 
Old November 23rd, 2008, 11:09 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Do you want to use XSLT 2.0 or 1.0?

--
  Martin Honnen
  Microsoft MVP - XML
 
Old November 23rd, 2008, 11:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0 use

<xsl:template match="root">
  <xsl:for-each-group select="*" group-starting-with="Level_1">
    <Level_1>
      <xsl:for-each-group select="current-group()" group-starting-with="Level_2">
        <Level_2>
          <xsl:for-each-group select="current-group()" group-starting-with="Level_3">

etc.

In XSLT 1.0 it's rather more difficult. Search for "positional grouping" or "sibling recursion".

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old November 23rd, 2008, 11:52 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Also if you have
Code:
<root>
  <Level_1>
        <Item_1></Item_1>
        <Item_2></Item_2>
  </Level_1>
  <Level_2>
        <Item_21></Item_21>
        <Item_22></Item_22>
  </Level_2>
  <Level_1>
        <Item_3></Item_3>
        <Item_3></Item_3>
  </Level_1>
  <Level_2>
        <Item_23></Item_23>
        <Item_24></Item_24>
  </Level_2>
</root>
should that become
Code:
<root>
  <Level_1>
        <Item_1></Item_1>
        <Item_2></Item_2>
        <Item_3></Item_3>
        <Item_3></Item_3>
        <Level_2>
        Ã‚  <Item_21></Item_21>
        Ã‚  <Item_22></Item_22>
        Ã‚  <Item_23></Item_23>
        Ã‚  <Item_24></Item_24>
        </Level_2>
  </Level_1>
</root>
or something else?

--
  Martin Honnen
  Microsoft MVP - XML
 
Old November 24th, 2008, 05:44 AM
Registered User
 
Join Date: Nov 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Martin Honnen
 Do you want to use XSLT 2.0 or 1.0?

--
Martin Honnen
Microsoft MVP - XML
Hi Martin!
I am using XSLT 2.0

 
Old November 24th, 2008, 05:54 AM
Registered User
 
Join Date: Nov 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 In XSLT 2.0 use

<xsl:template match="root">
<xsl:for-each-group select="*" group-starting-with="Level_1">
    <Level_1>
     <xsl:for-each-group select="current-group()" group-starting-with="Level_2">
        <Level_2>
         <xsl:for-each-group select="current-group()" group-starting-with="Level_3">

etc.

In XSLT 1.0 it's rather more difficult. Search for "positional grouping" or "sibling recursion".

Michael Kay

Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Hi Michael!

I tried to use your solution.
Here is my code:
Code:
    <xsl:template match="root">

        <xsl:for-each-group select="*" group-starting-with="Level_1">
            <Level_1>
                <Item_1>
                    <xsl:value-of select="Item_1"/>
                </Item_1>
                <Item_2>
                    <xsl:value-of select="Item_2"/>
                </Item_2>
                <xsl:for-each-group select="current-group()" group-starting-with="Level_2">
                    <Level_2>
                        <Item_21>
                            <xsl:value-of select="Item_21"/>
                        </Item_21>
                        <Item_22>
                            <xsl:value-of select="Item_22"/>
                        </Item_22>
                        <xsl:for-each-group select="current-group()" group-starting-with="Level_3">
                            <Level_3>
                                <Item_31>
                                    <xsl:value-of select="Item_31"/>
                                </Item_31>
                                <Item_32>
                                    <xsl:value-of select="Item_32"/>
                                </Item_32>
                            </Level_3>
                        </xsl:for-each-group>
                    </Level_2>
                </xsl:for-each-group>
            </Level_1>
        </xsl:for-each-group>
    </xsl:template>

It's partly working. Inserting blank nodes. For example:

Source:
Code:
<root>
    <Level_1>
        <Item_1>11</Item_1>
        <Item_2>22</Item_2>
    </Level_1>

    <Level_2>
        <Item_21>aa</Item_21>
        <Item_22>dd</Item_22>
    </Level_2>

    <Level_3>
        <Item_31>1234</Item_31>
        <Item_32>4321</Item_32>
    </Level_3>
    <Level_3>
        <Item_31>1235</Item_31>
        <Item_32>556</Item_32>
    </Level_3>
</root>
Target:

Code:
<Level_1>
    <Item_1>11</Item_1>
    <Item_2>22</Item_2>
    <Level_2>
        <Item_21></Item_21>
        <Item_22></Item_22>
        <Level_3>
            <Item_31></Item_31>
            <Item_32></Item_32>
        </Level_3>
    </Level_2>
    <Level_2>
        <Item_21>aa</Item_21>
        <Item_22>dd</Item_22>
        <Level_3>
            <Item_31></Item_31>
            <Item_32></Item_32>
        </Level_3>
        <Level_3>
            <Item_31>1234</Item_31>
            <Item_32>4321</Item_32>
        </Level_3>
        <Level_3>
            <Item_31>1235</Item_31>
            <Item_32>556</Item_32>
        </Level_3>
    </Level_2>
</Level_1>
 
Old November 24th, 2008, 05:56 AM
Registered User
 
Join Date: Nov 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Martin Honnen
 Also if you have
Code:
<root>
  <Level_1>
        <Item_1></Item_1>
        <Item_2></Item_2>
  </Level_1>
  <Level_2>
        <Item_21></Item_21>
        <Item_22></Item_22>
  </Level_2>
  <Level_1>
        <Item_3></Item_3>
        <Item_3></Item_3>
  </Level_1>
  <Level_2>
        <Item_23></Item_23>
        <Item_24></Item_24>
  </Level_2>
</root>
should that become
Code:
<root>
  <Level_1>
        <Item_1></Item_1>
        <Item_2></Item_2>
        <Item_3></Item_3>
        <Item_3></Item_3>
        <Level_2>
        Ã‚  <Item_21></Item_21>
        Ã‚  <Item_22></Item_22>
        Ã‚  <Item_23></Item_23>
        Ã‚  <Item_24></Item_24>
        </Level_2>
  </Level_1>
</root>
or something else?

--
  Martin Honnen
  Microsoft MVP - XML

That should become:
Code:
<root>
<Level_1>
        <Item_1></Item_1>
        <Item_2></Item_2>
                <Level_2>
                        <Item_21></Item_21>
                        <Item_22></Item_22>
                </Level_2>
</Level_1>

<Level_1>
        <Item_3></Item_3>
        <Item_4></Item_4>
                <Level_2>
                        <Item_23></Item_23>
                        <Item_24></Item_24>
                </Level_2>
</Level_1>

</root>
 
Old November 24th, 2008, 07:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This

<Item_31>
    <xsl:value-of select="Item_31"/>
</Item_31>

creates an empty element in the output if there is no element in the input. To avoid that, just replace it with

<xsl:copy-of select="Item_31"/>


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old November 24th, 2008, 08:47 AM
Registered User
 
Join Date: Nov 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 This

<Item_31>
    <xsl:value-of select="Item_31"/>
</Item_31>

creates an empty element in the output if there is no element in the input. To avoid that, just replace it with

<xsl:copy-of select="Item_31"/>


Michael Kay

Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Thanks for your help. Now my code is this:

Code:
<xsl:template match="root">

        <xsl:for-each-group select="*" group-starting-with="Level_1">
            <Level_1>

                <xsl:copy-of select="Item_1"/>
                <xsl:copy-of select="Item_2"/>

                <xsl:for-each-group select="current-group()" group-starting-with="Level_2">
                    <Level_2>

                        <xsl:copy-of select="Item_21"/>
                        <xsl:copy-of select="Item_22"/>

                        <xsl:for-each-group select="current-group()" group-starting-with="Level_3">
                            <Level_3>

                                <xsl:copy-of select="Item_31"/>
                                <xsl:copy-of select="Item_32"/>

                            </Level_3>
                        </xsl:for-each-group>
                    </Level_2>
                </xsl:for-each-group>
            </Level_1>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>
Now it's doing the job well, but there is only one strange thing.
In each node there is one empty next node. For example:

Source

Code:
<root>
    <Level_1>
        <Item_1>11</Item_1>
        <Item_2>22</Item_2>
    </Level_1>

    <Level_2>
        <Item_21>aa</Item_21>
        <Item_22>dd</Item_22>
    </Level_2>

    <Level_3>
        <Item_31>1234</Item_31>
        <Item_32>4321</Item_32>
    </Level_3>
    <Level_3>
        <Item_31>1235</Item_31>
        <Item_32>556</Item_32>
    </Level_3>

</root>
Target

Code:
<Level_1>
    <Item_1>11</Item_1>
    <Item_2>22</Item_2>
    <Level_2>
        <Level_3/>
    </Level_2>
    <Level_2>
        <Item_21>aa</Item_21>
        <Item_22>dd</Item_22>
        <Level_3/>
        <Level_3>
            <Item_31>1234</Item_31>
            <Item_32>4321</Item_32>
        </Level_3>
        <Level_3>
            <Item_31>1235</Item_31>
            <Item_32>556</Item_32>
        </Level_3>
    </Level_2>
</Level_1>
 
Old November 24th, 2008, 09:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I hope you've tried to understand my code and made an attempt to see where it was wrong - it's a bad idea to just use code without trying to understand it. The bug is where it does select="current-group()" - you only want to process all items in the current group after the first. You can do that using subsequence(current-group(),2).

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Flat structure to Nested dani1 XSLT 2 October 29th, 2008 02:51 AM
Convert nested XML to flat one dani1 XSLT 15 October 21st, 2008 02:20 AM
Help in making a FLAT xml to a structured xml shlomi XSLT 0 July 19th, 2007 07:58 AM
tree xml to flat xml whiteadi XSLT 2 November 14th, 2006 02:36 PM
translating a flat xml to a hierarchical xml stevea XSLT 4 June 13th, 2005 05:55 PM





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