[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSL transform to HTML when element names are unkno

Subject: Re: 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
xsl html in element
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.