Subject: [xsl] New approach to grouping problem that doesn't use keys or axes From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 17 Oct 2001 08:52:29 -0400 |
I have an XML file that includes a list of employees and their respective departments. I need to loop through the xml and sort the employees by department alphabetically.
I am not sure how to make the (Name) TreeNode element a child element of the (Department) TreeNode element and then populate all the employees within that department. ... Here is the XML file -
<NewDataSet> ... <cn>Belki Hercules</cn> <Department>ITl</Department> <Name>Belki Hercules</Name>
T:\jeff>type jeff.xml <NewDataSet> <Table> <cn>Acevedo, Antonia</cn> <Department>Dispatch-Freight</Department> <Name>Acevedo, Antonia</Name> </Table> <Table> <cn>Aimone, Charlie</cn> <Department>IT</Department> <Name>Aimone, Charlie</Name> </Table> <Table> <cn>Arenas, Lizalyn</cn> <Department>Dispatch-Freight</Department> <Name>Arenas, Lizalyn</Name> </Table> <Table> <cn>Arrendell, Miguel</cn> <Department>Warehouse</Department> <Name>Arrendell, Miguel</Name> </Table> <Table> <cn>Bayron, Jun</cn> <Department>Warehouse</Department> <Name>Bayron, Jun</Name> </Table> <Table> <cn>Belki Hercules</cn> <Department>IT</Department> <Name>Belki Hercules</Name> </Table> </NewDataSet>
T:\jeff>type jeff.xsl <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/"> <TREENODE> <xsl:apply-templates/> </TREENODE> </xsl:template>
<xsl:template match="NewDataSet"> <xsl:variable name="tables" select="Table"/> <xsl:for-each select="$tables"> <xsl:sort select="Department"/> <xsl:variable name="dept" select="Department"/> <xsl:if test="generate-id(.)= generate-id($tables[Department=$dept])"> <!--found first of each department in document order--> <TreeNode Text="{$dept}"> <xsl:for-each select="$tables[Department=$dept]"> <TreeNode Text="{Name}"/> </xsl:for-each> </TreeNode> </xsl:if> </xsl:for-each> </xsl:template>
T:\jeff>xt jeff.xml jeff.xsl <?xml version="1.0" encoding="utf-8"?> <TREENODE> <TreeNode Text="Dispatch-Freight"> <TreeNode Text="Acevedo, Antonia"/> <TreeNode Text="Arenas, Lizalyn"/> </TreeNode> <TreeNode Text="IT"> <TreeNode Text="Aimone, Charlie"/> <TreeNode Text="Belki Hercules"/> </TreeNode> <TreeNode Text="Warehouse"> <TreeNode Text="Arrendell, Miguel"/> <TreeNode Text="Bayron, Jun"/> </TreeNode> </TREENODE>
-- G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995) Web site: XSL/XML/DSSSL/SGML/OmniMark services, training, products. Book: Practical Transformation Using XSLT and XPath ISBN 1-894049-06-3 Article: What is XSLT? http://www.xml.com/pub/2000/08/holman/index.html Next public training (instructor-live, Internet-live, and web-based): -2001-10-22,11-01,11-02,11-05,11-19,11-21,12-03,12-05,12-09,12-10,12-19
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Build unique nodeset when, Thomas B. Passin | Thread | [xsl] Re: XSLT Date, Joerg Pietschmann |
[xsl] Build unique nodeset when Key, J S Publications | Date | [xsl] Re: XSLT Date, Joerg Pietschmann |
Month |