xsl-list
[Top] [All Lists]

Make Hirachy from flat file

2003-10-13 12:37:33
I have been readin some of the post about Adding Hirachy, it somehow theres
is somre resmblance to the problem I am trying to solve.

I have some XML data extracted from a ERP system into Excel and then saved
as Excel XML, it goes something like this.

<?xml version="1.0" encoding="utf-8"?>
<test>
        <row>
                <data>A</data>
                <data>1</data>
                <data>1234</data>
                <data>Description 1</data>
        </row>
        <row>
                <data>A</data>
                <data>1</data>
                <data>5678</data>
                <data>Description 2</data>
        </row>
        <row>
                <data>A</data>
                <data>2</data>
                <data>9012</data>
                <data>Description 3</data>
        </row>
        <row>
                <data>A</data>
                <data>2</data>
                <data>3456</data>
                <data>Description 4</data>
        </row>
        <row>
                <data>B</data>
                <data>1</data>
                <data>9012</data>
                <data>Description 3</data>
        </row>
        <row>
                <data>B</data>
                <data>2</data>
                <data>3456</data>
                <data>Description 4</data>
        </row>
</test>

So far I have only managed to get items grouped by the row/data[1] element,
but it is the "nested" gruoping by the row/data[2] element there is
bothering me.

Is there any hints how to get my XML look something like:

<test>
        <maingroup name="A">
                <prodgroup name="1">
                        <rows>
                                <itemno>1234</itemno>
                                <description>Description 1</description>
                        </rows>
                        <rows>
                                <itemno>5678</itemno>
                                <description>Description 2</description>
                        </rows>
                </prodgroup>
                <prodgroup name="2">
                        <rows>
                                <itemno>1234</itemno>
                                <description>Description 3</description>
                        </rows>
                        <rows>
                                <itemno>5678</itemno>
                                <description>Description 4</description>
                        </rows>
                </prodgroup>
        </maingroup>
        <maingroup name="B">
                <prodgroup name="1">
                        <rows>
                                <itemno>9012</itemno>
                                <description>Description 5</description>
                        </rows>
                </prodgroup>
                <prodgroup name="3">
                        <rows>
                                <itemno>3456</itemno>
                                <description>Description 6</description>
                        </rows>
                </prodgroup>
        </maingroup>
</test>

I only got this far:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
        <xsl:key name="groupkey" match="//test/row" use="data[1]"/>
        <xsl:template match="test">
                <test>
                        <xsl:apply-templates select="row[generate-id(.) =
generate-id(key('groupkey', data)[1])]"/>
                </test>
        </xsl:template>
        <xsl:template match="row">
                <xsl:element name="maingroup">
                        <xsl:attribute name="name"><xsl:value-of
select="data[1]"/></xsl:attribute>
                </xsl:element>
        </xsl:template>
</xsl:stylesheet>

Since I was hoping by using the generate-id, I could save a step and make
the loop on the prodroup name (row/data[2]), but for some strange reason to
me, I would have expected
        <row>
                <data>A</data>
                <data>1</data>
                <data>5678</data>
                <data>Description 2</data>
        </row>
To be different from
        <row>
                <data>B</data>
                <data>1</data>
                <data>9012</data>
                <data>Description 3</data>
        </row>
But I just can't seem to get i right.

</thomas>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



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