XSLT row/column transformation

XSLT row/column transformation

Post by John S. Daws » Sat, 26 Oct 2002 05:13:46



I am trying to do what is essentially a row/column transformation
using XSLT via MSXML. I have several rows of data that I want to
appear vertically in a HTML table cell (TD). I am able to get the
first set of rows to appear correctly but I don't know how to display
the others.

This problem is very hard to describe in text. I have an example of
what I am trying to do here:

http://www.technewsnow.com/xml/class.htm

Here is the output I am trying to produce with the transformation:

http://www.technewsnow.com/xml/class2.htm

In the example I have an XML island containing the data for a
fictitious class and XSL island for the transform. Under the class
there are students and under the students tests. Each test has three
values that I want to appear vertically under each test in the
resulting HTML. Essentially, I need to iterate over each test N times
to get the associated values. Each test will always have the same
number of values.

Any help is greatly appreciated.

Thanks in advance,
John S. Dawson

 
 
 

XSLT row/column transformation

Post by sauce » Sun, 27 Oct 2002 02:31:02


try to use double loops, for example:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
            <TABLE WIDTH="100%">
              <xsl:attribute name="class">Data</xsl:attribute>
              <xsl:apply-templates select="CLASSES/CLASS" />
            </TABLE>
</xsl:template>

<xsl:template match="CLASS">
  <TR>
         <TD><xsl:attribute name="colspan">100</xsl:attribute>
               <H3><xsl:value-of select="NAME"/></H3>
    </TD>
     </TR>
     <xsl:apply-templates select="STUDENTS/STUDENT" />
 <TR><TD>&#160;</TD></TR>
</xsl:template>

<xsl:template match="STUDENT">
  <xsl:variable name="student" select="." />
  <xsl:choose>
   <xsl:when test="position() != 1">
     <TR><TD>&#160;</TD></TR>
 </xsl:when>
  </xsl:choose>
   <TR>
  <TD>
   <xsl:attribute name="colspan">100</xsl:attribute>
   <B><xsl:value-of select="NAME"/></B>
  </TD>
 </TR>
 <TR>
     <TD>&#160;</TD>
  <xsl:for-each select="TESTS/TEST">
             <TD>
                        <B><U><xsl:value-of select="NAME"/></U></B>
                </TD>
        </xsl:for-each>
  </TR>
 <TR>
         <TD>
                      <xsl:attribute name="class">RowHeader</xsl:attribute>
                      <xsl:attribute name="nowrap">true</xsl:attribute>
                      <B>Date</B>
            </TD>
            <xsl:for-each select="TESTS/TEST">
                      <TD>
                        <xsl:value-of select="DATE"/>
                      </TD>
           </xsl:for-each>
   </TR>
    <xsl:for-each select="TESTS/TEST[1]/VALUES/VALUE">
    <xsl:variable name="pos" select="position()" />
   <TR>
    <TD>
     <B>Value <xsl:value-of select="$pos"/></B>
    </TD>
     <xsl:for-each select="$student/TESTS/TEST/VALUES/VALUE[$pos]">
     <TD><xsl:attribute name="class">DataLeft</xsl:attribute>
         <xsl:value-of select="."/>
     </TD>
     </xsl:for-each>
   </TR>
   </xsl:for-each>
</xsl:template>

</xsl:stylesheet>



Quote:> I am trying to do what is essentially a row/column transformation
> using XSLT via MSXML. I have several rows of data that I want to
> appear vertically in a HTML table cell (TD). I am able to get the
> first set of rows to appear correctly but I don't know how to display
> the others.

> This problem is very hard to describe in text. I have an example of
> what I am trying to do here:

> http://www.technewsnow.com/xml/class.htm

> Here is the output I am trying to produce with the transformation:

> http://www.technewsnow.com/xml/class2.htm

> In the example I have an XML island containing the data for a
> fictitious class and XSL island for the transform. Under the class
> there are students and under the students tests. Each test has three
> values that I want to appear vertically under each test in the
> resulting HTML. Essentially, I need to iterate over each test N times
> to get the associated values. Each test will always have the same
> number of values.

> Any help is greatly appreciated.

> Thanks in advance,
> John S. Dawson


 
 
 

XSLT row/column transformation

Post by sauce » Sun, 27 Oct 2002 02:35:54


sorry, for the inner loop, it is probably clearer if you use:

     <xsl:for-each select="$student/TESTS/TEST">
     <TD><xsl:attribute name="class">DataLeft</xsl:attribute>
         <xsl:value-of select="VALUES/VALUE[$pos]"/>
     </TD>
     </xsl:for-each>

although the result is same, :-)


Quote:> try to use double loops, for example:

> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="/">
>             <TABLE WIDTH="100%">
>               <xsl:attribute name="class">Data</xsl:attribute>
>               <xsl:apply-templates select="CLASSES/CLASS" />
>             </TABLE>
> </xsl:template>

> <xsl:template match="CLASS">
>   <TR>
>          <TD><xsl:attribute name="colspan">100</xsl:attribute>
>                <H3><xsl:value-of select="NAME"/></H3>
>     </TD>
>      </TR>
>      <xsl:apply-templates select="STUDENTS/STUDENT" />
>  <TR><TD>&#160;</TD></TR>
> </xsl:template>

> <xsl:template match="STUDENT">
>   <xsl:variable name="student" select="." />
>   <xsl:choose>
>    <xsl:when test="position() != 1">
>      <TR><TD>&#160;</TD></TR>
>  </xsl:when>
>   </xsl:choose>
>    <TR>
>   <TD>
>    <xsl:attribute name="colspan">100</xsl:attribute>
>    <B><xsl:value-of select="NAME"/></B>
>   </TD>
>  </TR>
>  <TR>
>      <TD>&#160;</TD>
>   <xsl:for-each select="TESTS/TEST">
>              <TD>
>                         <B><U><xsl:value-of select="NAME"/></U></B>
>                 </TD>
>         </xsl:for-each>
>   </TR>
>  <TR>
>          <TD>
>                       <xsl:attribute

name="class">RowHeader</xsl:attribute>

- Show quoted text -

>                       <xsl:attribute name="nowrap">true</xsl:attribute>
>                       <B>Date</B>
>             </TD>
>             <xsl:for-each select="TESTS/TEST">
>                       <TD>
>                         <xsl:value-of select="DATE"/>
>                       </TD>
>            </xsl:for-each>
>    </TR>
>     <xsl:for-each select="TESTS/TEST[1]/VALUES/VALUE">
>     <xsl:variable name="pos" select="position()" />
>    <TR>
>     <TD>
>      <B>Value <xsl:value-of select="$pos"/></B>
>     </TD>
>      <xsl:for-each select="$student/TESTS/TEST/VALUES/VALUE[$pos]">
>      <TD><xsl:attribute name="class">DataLeft</xsl:attribute>
>          <xsl:value-of select="."/>
>      </TD>
>      </xsl:for-each>
>    </TR>
>    </xsl:for-each>
> </xsl:template>

> </xsl:stylesheet>



> > I am trying to do what is essentially a row/column transformation
> > using XSLT via MSXML. I have several rows of data that I want to
> > appear vertically in a HTML table cell (TD). I am able to get the
> > first set of rows to appear correctly but I don't know how to display
> > the others.

> > This problem is very hard to describe in text. I have an example of
> > what I am trying to do here:

> > http://www.technewsnow.com/xml/class.htm

> > Here is the output I am trying to produce with the transformation:

> > http://www.technewsnow.com/xml/class2.htm

> > In the example I have an XML island containing the data for a
> > fictitious class and XSL island for the transform. Under the class
> > there are students and under the students tests. Each test has three
> > values that I want to appear vertically under each test in the
> > resulting HTML. Essentially, I need to iterate over each test N times
> > to get the associated values. Each test will always have the same
> > number of values.

> > Any help is greatly appreciated.

> > Thanks in advance,
> > John S. Dawson

 
 
 

XSLT row/column transformation

Post by John S. Daws » Thu, 31 Oct 2002 08:00:48


That worked perfectly. You are a life saver! Thanks a million.

John