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

Subject: [xsl] XSL transform to HTML when element names are unknown
From: "Tusler, Dylan" <tuslerd@xxxxxxxxxxxxxxxxxxx>
Date: Wed, 4 Jan 2006 13:05:26 +1000
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?

I have not done much XSL for a few years, and am slowly getting back into it,
so I apologise if the code below is a bit clumsy. I would also love any
suggestions on improving what I've already got.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output omit-xml-declaration="yes" method="html" />
<xsl:template match="//Tables/*">
	<table border="1">
	<xsl:choose>
	<xsl:when test="name(.) = name(preceding-sibling::*[1])">
	<tr>
		<xsl:for-each select="*">
			<td>
			<xsl:value-of select="." />
			</td>
		</xsl:for-each>
	</tr>
	</xsl:when>
	<xsl:otherwise>
		<b><xsl:value-of select="name(.)"/></b>
		<tr>
			<xsl:for-each select="*">
				<td>
				<xsl:value-of select="name(.)" />
				</td>
			</xsl:for-each>
		</tr>
		<tr>
			<xsl:for-each select="*">
				<td>
				<xsl:value-of select="." />
				</td>
			</xsl:for-each>
		</tr>
	</xsl:otherwise>
	</xsl:choose>
	</table>
</xsl:template>
</xsl:stylesheet>

Dylan Tusler
Systems/Integration Analyst - Information Technology
Maroochy Shire Council
ph: +61 (0)7 5441 8202
"Your Technology Solutions Partner"


=======================================================
The contents of this message may express views and opinions not necessarily
shared by Maroochy Shire Council. This email and any files transmitted with it
are intended solely for the use of the individual or entity to whom they are
addressed.
Maroochy Shire Council is a government organisation and is subject to Freedom
of Information legislation. You must assume that any material sent to Maroochy
Shire Council may be monitored and may be accessible by persons other than the
intended recipient.
=======================================================
Note: Maroochy Shire Council does not accept multimedia or vulnerable file
types such as executables, html or GIF files via email.
Emails must be in English and under 10MB.
mscmail5.1

Current Thread