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

Re: Transpose of Table

Subject: Re: Transpose of Table
From: yguaba@xxxxxxxxxxxx
Date: Wed, 24 Dec 2003 15:43:19 -0200
html table transpose
Hello Animesh,

That's an interesting problem, and even though I'm sure someone's 
solved it before, it's a good opportunity to learn a little myself, 
so I'll give it a try.

Here's what I've come up with:


<!-- START OF STYLESHEET -->

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="html" indent="yes" encoding="iso-8859-1"/>

<xsl:template match="/">
	<html>
		<body>
			<xsl:apply-templates/>
		</body>
	</html>
</xsl:template>

<xsl:template match="/html/body/table">
	<table>
		<xsl:attribute name="border">
			<xsl:value-of select="@border"/>
		</xsl:attribute>
		<xsl:for-each select="tr[position()=1]/td">
			<xsl:variable name="counter" select="position()"/>
			<tr>
				<xsl:for-each select="ancestor::table/tr/td[position() = 
$counter]">
					<td>
						<xsl:value-of select="."/>
					</td>
				</xsl:for-each>
			</tr>
		</xsl:for-each>
	</table>
</xsl:template>

<xsl:template match="p">
	<p>
		<xsl:value-of select="."/>
	</p>
</xsl:template>

</xsl:stylesheet>

<!-- END OF STYLESHEET -->


It's probably not the most elegant or economical solution, but it'll 
get the job done. A word of caution: I've converted your TR and TD 
elements to lowercase, and as XML is case-sensitive, you'll need to 
either change the case of these elements in the XML file to lower 
case or in the stylesheet to upper case.

Merry Xmas,

Erik


On 24 Dec 2003 at 11:47, Animesh Sharma wrote:

> I want to write a XSL which will output me the transpose of existing
> XHTML.
> 
> Suppose I have input something like:
> 
> <?xml version="1.0"?>
> <html>
>    <body>
>       <p>Test Table</p>
>  <table border="1">
>   <TR>
>    <TD>1 </TD>
>    <TD>2 </TD>
>    <TD>3 </TD>
>   </TR>
>   <TR>
>    <TD>4 </TD>
>    <TD>5 </TD>
>    <TD>6 </TD>
>   </TR>	
>   <TR>
>    <TD>7 </TD>
>    <TD>8 </TD>
>    <TD>9 </TD>
>   </TR>	
>  </table>  
>  </body>
> </html>
> 
> i.e
> 
> Test Table
> 1 2 3
> 4 5 6
> 7 8 9
> 
> I want to write an XSl which will give me transpose of above table and
> output will look like:
> 
> Test Table
> 1 4 7
> 2 5 8
> 3 6 9
> 
> i.e.
> <?xml version="1.0"?>
> <html>
>    <body>
>       <p>Test Table</p>
>  <table border="1">
>   <TR>
>    <TD>1 </TD>
>    <TD>4 </TD>
>    <TD>5 </TD>
>   </TR>
>   <TR>
>    <TD>2 </TD>
>    <TD>5 </TD>
>    <TD>8 </TD>
>   </TR>	
>   <TR>
>    <TD>3 </TD>
>    <TD>6 </TD>
>    <TD>9 </TD>
>   </TR>	
>  </table>  
>  </body>
> </html>
> 
> Well, I want to write a generic XSL which can work for any n*n table.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.