|
top
|
 Subject: Nesting Tables and Sub-Tables Author: Ivan Pedruzzi Date: 28 Jan 2005 12:56 AM
|
Hi Greg,
Assuming your database looks like this
Contact (ID, CONTACT_NAME)
Group (ID, GROUP_NAME)
ContactGroup (ContactID, GroupID)
Running the following query using the Stylus Studio SQL/XML editor (DB to XML Data Source)
SELECT
XMLELEMENT(name "GROUP",
XMLELEMENT(name "NAME", g.GROUP_NAME),
(
SELECT
XMLELEMENT(name "CONTACTS",
XMLELEMENT(name "NAME", c.CONTACT_NAME)
)
FROM Contact c, ContactGroup cg WHERE cg.GroupID = g.ID AND cg.ContactID = c.ID
)
)
FROM Group g
Generates the following XML result
<root>
<GROUP>
<NAME>AAA</NAME>
<CONTACTS>
<NAME>John</NAME>
<NAME>Bill</NAME>
</CONTACTS>
</GROUP>
…
</root>
Now you can transform the above document using the XSLT below to generate an HTML page
Hope this helps
Ivan
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html><head/>
<body>
<table width="100%" border="1">
<tbody>
<xsl:for-each select="root/GROUP">
<tr>
<td width="50%">
<xsl:value-of select="NAME"/>
</td>
<td width="50%">
<table width="100%" border="1">
<tbody>
<xsl:for-each select="CONTACTS/NAME">
<tr>
<td width="50%">
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
|
|