[xsl] Re: Transpose of Table

Subject: [xsl] Re: Transpose of Table
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Wed, 24 Dec 2003 11:36:42 +0100
"Animesh Sharma" <asharma@xxxxxxxxxxxxxxxx> wrote in message
news:313D89F0454D6F409E5A6303D69D16A11CC197@xxxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> I want to write a XSL which will output me the transpose of existing
XHTML.

[snip]

> Well, I want to write a generic XSL which can work for any n*n table.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="table">
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:for-each select="TR[1]/TD">
        <xsl:variable name="vPos" select="position()"/>
        <TR>
          <xsl:for-each
            select="../../TR/TD[position() = $vPos]">
            <TD><xsl:value-of select="."/></TD>
          </xsl:for-each>
        </TR>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<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>


produces the wanted result:

<html>
   <body>
      <p>Test Table</p>
      <table border="1">
         <TR>
            <TD>1 </TD>
            <TD>4 </TD>
            <TD>7 </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>


Dimitre Novatchev.
FXSL developer

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html




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


Current Thread