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 June 17th, 2005, 10:37 AM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT - 2 level grouping & sequencing

Hi,

I need a help in doing 2 level grouping (& sequencing - mentioned later) on my XML - Thanks in advance for any input :

I - 2 level grouping
input XML:
<?xml version="1.0" encoding="utf-8"?>
<File>
<Level1>
     <L1key>100</L1key>
    <Level2><L2key>01</L2key></Level2>
</Level1>
<Level1>
     <L1key>200</L1key>
    <Level2><L2key>02</L2key></Level2>
</Level1>
<Level1>
    <L1key>100</L1key>
    <Level2><L2key>02</L2key></Level2>
</Level1>
<Level1>
     <L1key>300</L1key>
    <Level2><L2key>03</L2key></Level2>
</Level1>
<Level1>
     <L1key>100</L1key>
    <Level2><L2key>08</L2key></Level2>
</Level1>

Expected output XML:

<?xml version="1.0" encoding="utf-8"?>
<File>
<L1>
    <L1key>100</L1key>
    <L2><L2key>01</L2key></L2>
    <L2><L2key>02</L2key></L2>
    <L2><L2key>08</L2key></L2>
</L1>
<L1>
    <L1key>200</L1key>
    <L2><L2key>02</L2key></L2>
</L1>
<L1>
     <L1key>300</L1key>
    <L2><L2key>03</L2key></L2>
</L1>
</File>

Description:
As you may notice from the above, I want to group all the level2's that have the same Level1 keys. (see the one with L1key value 100).
How do I do that? My XSLT is given below - but it only displays one Level2 (not all of them)

XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:key name="L1key" match="Level1" use="L1key"/>
    <xsl:template match="/File">
        <File>
            <xsl:attribute name="xsi:noNamespaceSchemaLocation">C:/BTPOC2~1/XSLTTE~1/out_xsd.xsd</xsl:attribute>

            <xsl:for-each select="Level1[generate-id()=generate-id(key('L1key',L1key))]">
                    <L1>
                        <L1key>
                            <xsl:for-each select="L1key">
                            <xsl:value-of select="."/>
                            </xsl:for-each>
                        </L1key>

                            <xsl:for-each select="Level2">
                                <L2>
                                    <xsl:for-each select="L2key">
                                        <L2key>
                                            <xsl:value-of select="."/>
                                        </L2key>
                                    </xsl:for-each>
                                </L2>
                            </xsl:for-each>

                    </L1>
            </xsl:for-each>
        </File>
    </xsl:template>
</xsl:stylesheet>


II - sequencing:

In the above, for example, if I expand the output XML - level 2 as
<L2><L2key seq="1">01</L2key></L2>, i.e. to introduce the sequence # to identify each L2key value uniquely , how do I do that?

Thanks,
Ken

 
Old June 17th, 2005, 10:46 AM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi - I forgot to mention that I would like to do this uding XSLT 1.0 Thanks...

 
Old June 21st, 2005, 02:34 AM
Authorized User
 
Join Date: May 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to meetnaveen4u
Default


In the above, for example, if I expand the output XML - level 2 as
<L2><L2key seq="1">01</L2key></L2>, i.e. to introduce the sequence # to identify each L2key value uniquely , how do I do that?

This one is not clear, do u mean to say that u need to group second level using sequence id for that key??. or u need to group only L2key value.

Thanks &amp; Regards
Naveen.
 
Old June 27th, 2005, 01:23 PM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I resolved both the items i.e.
1) grouping &
2) adding a sequence # to the child nodes

Thanks,
Ken






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Going up a level from current node. lafilip XSLT 4 February 23rd, 2007 03:06 PM
Grouping in XSLT at same level of hierarchy... concen XSLT 2 April 6th, 2006 10:12 AM
XSLT 1.0 Grouping kwilliams XSLT 0 January 11th, 2006 06:30 PM
XSLT Grouping Help Missy XSLT 0 December 14th, 2005 10:28 PM
Non-recursive Data multi-level grouping jmurdock BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 June 30th, 2004 03:42 PM





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