xsl-list
[Top] [All Lists]

[xsl] grouping items in a list of records

2009-05-20 03:33:07
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 Škopík
 

__________ 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
 

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>