xsl-list
[Top] [All Lists]

Re: Transpose of Table

2003-12-24 10:43:19
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



<Prev in Thread] Current Thread [Next in Thread>