xsl-list
[Top] [All Lists]

Tyro grouping and nesting problem

2005-02-11 11:46:37
Being a complete xsl tyro as I am, I know I'm making some sort of conceptual error. This is clearly the wrong way to go about this. Has anybody got a clue for the clueless?

Starting with an xml file like this:
<body>
        <item>
                <date>today</date>
                <name>James</name>
                <place>market</place>
        </item>
        <item>
                <date>today</date>
                <name>Joe</name>
                <place>work</place>
        </item>
        <item>
                <date>today</date>
                <name>Joe</name>
                <place>school</place>
        </item>
        <item>
                <date>tomorrow</date>
                <name>Jean</name>
                <place>home</place>
        </item>
        <item>
                <date>thenextday</date>
                <name>Jon</name>
                <place>work</place>
        </item>
</body>

This is the result I want to get:
<body>
        <today>
                <item>
                        <date>today</date>
                        <name>James</name>
                        <place>market</place>
                </item>
                <item>
                        <date>today</date>
                        <name>Joe</name>
                        <place>work</place>
                </item>
                <item>
                        <date>today</date>
                        <name>Joe</name>
                        <place>school</place>
                </item>
        </today>
        <tomorrow>
                <item>
                        <date>tomorrow</date>
                        <name>Jean</name>
                        <place>home</place>
                </item>
        </tomorrow>
        <thenextday>
                <item>
                        <date>thenextday</date>
                        <name>Jon</name>
                        <place>work</place>
                </item>
        </thenextday>
</body>

Using the Muenchian method, I've been able to identify unique values for item/date thus:
<xsl:key name="items" match="item" use="date"/>
<xsl:template match="/">
<xsl:param name="uniquedates" select="//item[generate-id(.)=generate-id(key('items',date))]"/>
        <xsl:for-each select="$uniquedates">
                <xsl:element name="{string(date)}">
                        <xsl:copy-of select="."/>
                </xsl:element>
        </xsl:for-each>
</xsl:template>

But, since $uniquedates returns a node set containing only three nodes, I can't figure out how to populate the new elements with all of its appropriate children, so I get this:
<body>
        <today>
                <item>
                        <date>today</date>
                        <name>James</name>
                        <place>market</place>
                </item>
        </today>
        <tomorrow>
                <item>
                        <date>tomorrow</date>
                        <name>Jean</name>
                        <place>home</place>
                </item>
        </tomorrow>
        <thenextday>
                <item>
                        <date>thenextday</date>
                        <name>Jon</name>
                        <place>work</place>
                </item>
        </thenextday>
</body>
I know I'm missing something obvious here. Any sort of clue would be welcome.
__________
J.J. Crump
Dept. of History 353560
University of Washington
Seattle, WA. 98195


--~------------------------------------------------------------------
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>
--~--