[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

grouping items in a list of records

Subject: grouping items in a list of records
From: "Skopik Pavel" <Pavel.Skopik@xxxxxxxxxx>
Date: Wed, 20 May 2009 09:22:55 +0200
 grouping items in a list of records
Hello,

tried to post this message yesterday, but it did not show up in list. So I am
trying again. Sorry if it gets doubled.

Recently I have come across a grouping issue I am not able to solve.

I am creating XSLT stylesheets which transforms search results to
Wordprocessing ML. The input XML is a list of records. Within each record I
need to group items according to certain criteria. I have been able to group
these items for one record only (in other stylesheet which is part of the
application). But when it comes to multiple records the method I used does not
work.

I use XSLT 1.0 with Xalan.

Bellow is example XML data I need to group with further explanation:

<data>
    <datasource>
        <item>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 1</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Orders</name>
                    </name>
                    <order>2</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 2</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Orders</name>
                    </name>
                    <order>3</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 3</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Approves</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 4</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Orders</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 5</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Approves</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
        </item>
        <item>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 5</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Organizes</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 6</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Organizes</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 7</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Executes</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 8</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Executes</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
            <planItemOrganization>
                <organization>
                    <name1>
                        <name>Organization 9</name>
                    </name1>
                </organization>
                <role>
                    <code>
                        <codeNumber>1</codeNumber>
                    </code>
                    <name>
                        <name>Co-operation</name>
                    </name>
                    <order>1</order>
                </role>
            </planItemOrganization>
        </item>
    </datasource>
</data>

Within each "item" I need to group the node "planItemOrganization" according
to "role" and for each role output value represented by node
"organization/name1/name". The thing is that values of the role node are not
constant and they may change.

For now the stylesheets finds all roles according to key, groups organizations
right but outputs all in the first item which is not desired. I am wondering
how to restrict the processing to just current item, so that within each item
groups are created in the right way. In XSLT 2.0 it would be fairly easy, but
I am stuck with 1.0...

This is the stylesheet which I succesfully used for grouping in one record now
adapted to process list of records (for better readablity and simplification I
extracted it to ouptut text):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

    <xsl:key name="role" match="planItemOrganization" use="role/name/name"/>

    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match="data">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="datasource">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="item">
        <xsl:for-each select="planItemOrganization[count(. | key('role',
role/name/name)[1]) = 1]">
            <xsl:sort select="order"/>
            <xsl:text>&#10;</xsl:text>
            <xsl:value-of select="role/name/name"/>
            <xsl:text>&#10;</xsl:text>
            <xsl:for-each select="key('role', role/name/name)">
                <xsl:text>  </xsl:text>
                <xsl:value-of select="organization/name1/name"/>
                <xsl:text>&#10;</xsl:text>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

So the ouput should look like this:

Item 1
Orders
  Organization 5
  Organization 2
  Organization 4

Approves
  Organization 3
  Organization 5

Co-operation
  Organization 9

Item 2
Organizes
  Organization 5
  Organization 6

Executes
  Organization 7
  Organization 8

Co-operation
  Organization 9

Instead, it looks like this:

Item 1
Orders
  Organization 5
  Organization 2
  Organization 4

Approves
  Organization 3
  Organization 5

Co-operation
  Organization 9
  Organization 9
  Organization 9

Organizes
  Organization 5
  Organization 6
  Organization 5
  Organization 6

Executes
  Organization 7
  Organization 8
  Organization 7
  Organization 8

Item 2 is empty.

Is there a way how to achieve this?

Thank you for ideas.

Pavel )kopmk


__________ Informace od ESET NOD32 Antivirus, verze databaze 4089 (20090519)
__________

Tuto zpravu proveril ESET NOD32 Antivirus.

http://www.eset.cz



__________ Informace od ESET NOD32 Antivirus, verze databaze 4089 (20090519)
__________

Tuto zpravu proveril ESET NOD32 Antivirus.

http://www.eset.cz

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.