RE: [xsl] Dynamic Table

Subject: RE: [xsl] Dynamic Table
From: Marian Olteanu <mou_softwin@xxxxxxxxx>
Date: Mon, 20 Dec 2004 10:41:31 -0800 (PST)
Try this:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="html"/>

<xsl:param name="widthTable" select="3"/>
<xsl:param name="widthCell" select="3"/>

<xsl:template match="/">
	<table>
		<xsl:text disable-output-escaping="yes">&lt;tr&gt;</xsl:text>
		<xsl:call-template name="processNode">
			<xsl:with-param name="node" select="/domain/variable[1]"/>
			<xsl:with-param name="usedCols" select="0"/>
		</xsl:call-template>
		<xsl:text disable-output-escaping="yes">&lt;/tr&gt;</xsl:text>
	</table>
</xsl:template>

<xsl:template name="processNode">
	<xsl:param name="node"/>
	<xsl:param name="usedCols" select="0"/>
	
	<xsl:variable name="nodeWidth" select="string-length( normalize-space( $node/text() ) )"/>
	<xsl:variable name="nodeWidthInCells">
		<xsl:choose>
			<xsl:when test="$nodeWidth mod $widthCell = 0">
				<xsl:value-of select="$nodeWidth div $widthCell"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="1 + floor($nodeWidth div $widthCell)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<!-- check if new line -->
	<xsl:if	test="( $widthTable - $usedCols ) &lt; $nodeWidthInCells ">
		<xsl:text disable-output-escaping="yes">&lt;/tr&gt;</xsl:text>
		<xsl:text disable-output-escaping="yes">&lt;tr&gt;</xsl:text>
	</xsl:if>

	<td>
		<xsl:if test="$nodeWidthInCells &gt; 1">
			<xsl:attribute name="colspan">
				<xsl:value-of select="$nodeWidthInCells"/>
			</xsl:attribute>
		</xsl:if>
		<xsl:value-of select="normalize-space( $node/text() )"/>
	</td>


	<xsl:if test="$node/following-sibling::variable[1]">
		<xsl:call-template name="processNode">
			<xsl:with-param name="node" select="$node/following-sibling::variable[1]"/>
			<xsl:with-param name="usedCols">
				<xsl:choose>
					<xsl:when test="( $widthTable - $usedCols ) &lt; $nodeWidthInCells ">
						<xsl:value-of select="$nodeWidthInCells"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$nodeWidthInCells + $usedCols"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:if>
</xsl:template>
</xsl:stylesheet>



--- Pieter Reint Siegers Kort <pieter.siegers@xxxxxxxxxxx> wrote:

> Hi Frangois,
> 
> Check out:
> http://www.dpawson.co.uk/xsl/sect2/N7450.html
> 
> HTH,
> <prs/> 
> 
> -----Original Message-----
> From: Frangois Torche [mailto:torche@xxxxxxxxxx] 
> Sent: Monday, December 20, 2004 11:21 AM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Dynamic Table
> 
> Hello,
> 
> I have a very basic XML document that looks like:
> <domain>
>   <variable> abc </variable>
>   <variable> def </variable>
>   <variable> ghi </variable>
>   <variable> abcdefgh </variable>
>   <variable> abcdef </variable>
>   <variable> abc </variable>
>   ...
> </domain>
> 
> And I would like to create an HTML table with, for example, 3 columns. Each
> column can contain 3 characters max. The result of my transformation should
> look like:
> <table>
>   <tr>
>     <td> abc </td>
>     <td> def </td>
>     <td> def </td>
>   </tr>
>   <tr>
>     <td colspan = "3"> abcdefgh </td>
>   </tr>
>   <tr>
>     <td colspan = "2"> abcdef </td>
>     <td> abc </td>
>   </tr>
> </table>
> 
> I don't have a clue how to dynamically determine the colspan attribute and
> start a new row if there is not enough room to put the content of a
> variable.
> 
> Many thanks in advance for your help,
> Frangois
> 
> 


=====
Marian
http://www.utdallas.edu/~mgo031000/


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Helps protect you from nasty viruses. 
http://promotions.yahoo.com/new_mail

Current Thread