Re: [xsl] XSL transform to HTML when element names are unknown

Subject: Re: [xsl] XSL transform to HTML when element names are unknown
From: andrew welch <andrew.j.welch@xxxxxxxxx>
Date: Wed, 4 Jan 2006 09:50:10 +0000
On 1/4/06, Tusler, Dylan <tuslerd@xxxxxxxxxxxxxxxxxxx> wrote:
> Happy new year!
>
> I have XML that looks something like this:
> <Tables>
>         <FirstTableName>
>                 <feqnum>ME6698</feqnum>
>                 <feqt1>    22.8</feqt1>
>                 <fmet>686.96</fmet>
>                 <datem>2005-01-20T10:43:00.0000000+10:00</datem>
>                 <fr>*1</fr>
>                 <loc>*19119</loc>
>                 <tdate>2005-01-20T10:44:29.9400000+10:00</tdate>
>                 <enterby>TOD</enterby>
>                 <eqt3>2005-02-01T00:00:00.0000000+10:00</eqt3>
>         </FirstTableName>
>         <SecondTableName>
>                 <ref2>ME6698</ref2>
>                 <datei1>2005-01-04T00:00:00.0000000+10:00</datei1>
>                 <units1>24</units1>
>                 <amt1>-723.12</amt1>
>         </SecondTableName>
>         <SecondTableName>
>                 <ref2>ME6698</ref2>
>                 <datei1>2005-01-04T00:00:00.0000000+10:00</datei1>
>                 <units1>45.6</units1>
>                 <amt1>-1373.93</amt1>
>         </SecondTableName>
>         <SecondTableName>
>                 <ref2>ME6698</ref2>
>                 <datei1>2005-01-04T00:00:00.0000000+10:00</datei1>
>                 <units1>7.6</units1>
>                 <amt1>-228.99</amt1>
>         </SecondTableName>
> </Tables>
>
> Rules about this data:
> 1) There is always a root node called "Tables"
> 2) There can be one or more subelements. The names vary. In this example
they are called "FirstTableName" and "SecondTableName" but there could be
others.
> 3) These 'table' elements will have one or more subelements. The number of
subelements should be fixed for each table, and the names of the elements will
always be the same for each 'table', but the number and names of the elements
will vary from table to table.
>
> I am trying to apply an XSL transform to present this data in HTML tables
(along with the column names.) The best I can do so far is copied below.
However, this draws a separate table around each row, resulting in disjointed
rows with differing widths. It is close, but not quite what I need. Can anyone
suggest a way to draw a separate table around each of the sets of data?

How about:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:key name="tableData" match="Tables/*" use="local-name()"/>

<xsl:template match="/">
	<body>
		<xsl:apply-templates select="/Tables/*[generate-id() =
generate-id(key('tableData', local-name())[1])]"/>
	</body>
</xsl:template>

<xsl:template match="*">
	<table>
		<xsl:apply-templates select="key('tableData', local-name())"
mode="content"/>
	</table>
</xsl:template>

<xsl:template match="*" mode="content">
	<tr>
		<xsl:for-each select="*">
			<td><xsl:value-of select="."/></td>
		</xsl:for-each>
	</tr>
</xsl:template>

</xsl:stylesheet>

This groups the elements by their name, then processes all the
children of elements with the same name.  The output is:

<body>
	<table>
		<tr>
			<td>ME6698</td>
			<td>    22.8</td>
			<td>686.96</td>
			<td>2005-01-20T10:43:00.0000000+10:00</td>
			<td>*1</td>
			<td>*19119</td>
			<td>2005-01-20T10:44:29.9400000+10:00</td>
			<td>TOD</td>
			<td>2005-02-01T00:00:00.0000000+10:00</td>
		</tr>
	</table>
	<table>
		<tr>
			<td>ME6698</td>
			<td>2005-01-04T00:00:00.0000000+10:00</td>
			<td>24</td>
			<td>-723.12</td>
		</tr>
		<tr>
			<td>ME6698</td>
			<td>2005-01-04T00:00:00.0000000+10:00</td>
			<td>45.6</td>
			<td>-1373.93</td>
		</tr>
		<tr>
			<td>ME6698</td>
			<td>2005-01-04T00:00:00.0000000+10:00</td>
			<td>7.6</td>
			<td>-228.99</td>
		</tr>
	</table>
</body>

cheers
andrew

Current Thread