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 21st, 2006, 06:23 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Muenchian Grouping

Hi there,

I am trying to group attributes in <RelatedEntityRef> together that belongs to the same parent id [@RelatedID]. But the XSLT, I have written so far seems to be doing part of the job. Could someone kindly point out where I am going wrong?

The source XML looks like this:

<projects>
    <ValueItem Id="1">
        <PercentOwned>
            <RelatedEntityRef RelatedID="1" name="aaa" number="3.92"/>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="2" name="bbb" project_name="3.95"/>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="1" name="ccc" type="txt"/>
        </PercentOwned>
    </ValueItem>
    <ValueItem Id="2">
        <PercentOwned>
            <RelatedEntityRef RelatedID="1" type="jpg"/>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="2" name="ddd" project_name="3.96"/>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="1" project_name="3.92"/>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="3" name="fff" project_name="3.95"/>
        </PercentOwned>
    </ValueItem>
</projects>


My desired result should like this:

<projects>
    <ValueItem Id="1">
        <PercentOwned>
            <RelatedEntityRef RelatedID="1" number="3.92" type="txt">
                <name>aaa</name>
                <name>ccc</name>
            </RelatedEntityRef>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="2" project_name="3.95">
                <name>bbb</name>
            </RelatedEntityRef>
        </PercentOwned>
    </ValueItem>
    <ValueItem Id="2">
        <PercentOwned>
            <RelatedEntityRef RelatedID="1" type="jpg" project_name="3.92"/>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="2" project_name="3.96">
                <name>ddd</name>
            </RelatedEntityRef>
        </PercentOwned>
        <PercentOwned>
            <RelatedEntityRef RelatedID="3" project_name="3.95">
                <name>fff</name>
            </RelatedEntityRef>
        </PercentOwned>
    </ValueItem>
</projects>

My XSLT looks like this:

<xsl:key name="rows" match="RelatedEntityRef" use="@RelatedID"/>

<xsl:template match="PercentOwned" mode="LIXI">

    <xsl:copy>
        <xsl:apply-templates mode="LIXI" select="RelatedEntityRef[generate-id(.)=generate-id(key('rows',@RelatedID)[1])]"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="RelatedEntityRef" mode="LIXI">
    <xsl:copy>
        <xsl:for-each select="key('rows',@RelatedID)">
            <xsl:apply-templates select="@*"/>
            <Name> <xsl:value-of select = "@Name"/></Name>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

My output only seems to be working for the first <ValueItem> but not quite as it is also picking up values from the next <ValueItem>. In the 2nd <ValueItem>, I just get blank elements.

Hope this is not too much to explain. thanks
 
Old June 21st, 2006, 10:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't shown your key definition. However, keys are document-wide so to do grouping on subsets of a document you need to add information that makes each key unique within the document. In yor case, something like use="concat(@RelatedId, '~', ../../@Id) might do it. Of course you need to construct the concatenated key when you use the key() function as well.

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
help with Muenchian method grouping and html table mickhughes XSLT 3 May 1st, 2008 06:24 AM
help with Muenchian method grouping... agentdz015 XSLT 1 April 7th, 2008 04:53 PM
Muenchian grouping amhicraig XSLT 1 December 5th, 2007 06:43 PM
Advanced Grouping Using the Muenchian Method Bodiam XSLT 0 August 8th, 2005 11:33 AM
Grouping XML by Muenchian Method - Urgent :-( xrow XSLT 5 September 28th, 2004 04:07 AM





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