xsl-list
[Top] [All Lists]

Transforming XML to HTML Table

2003-01-29 05:00:03
Hi,

This might be a bit of a newbie's question but I can't find the answer 
anywhere....

I have an ASP.NET page with an XML webcontrol on it, to which I pass an XML 
document and an XSLT document. I am trying to transform the data in the XML  
document into an HTML table. The XML is in the following format:

<report>
    <performance>
        <summary>
            .....
        </summary>
        <data>
            <row>
                <datetime>01/01/2003 00:00</datetime>
                <sample>
                    <label>sample 1</label>
                    <displayval>0.001</displayval>
                </sample>
                <sample>
                    <label>Sample 2</label>
                    <displayval>0.345</displayval>
                </sample>
                <sample>
                    <label>Sample 3</label>
                    <displayval>0.763</displayval>
                </sample>
            </row>
            <row>
                <datetime>01/01/2003 00:30</datetime>
                <sample>
                    <label>sample 3</label>
                    <displayval>0.694</displayval>
                </sample>
                <sample>
                    <label>Sample 1</label>
                    <displayval>0.002</displayval>
                </sample>
                <sample>
                    <label>Sample 2</label>
                    <displayval>0.348</displayval>
                </sample>
            </row>
        </data>
    </performance>
</report>

For each report the same samples are present in each row, but the samples vary 
in number and label between reports.
I need to create an HTML table from each report where each <row> is a row in 
the table, and the samples in each row are the columns in that row i.e.

<TABLE>
  <TR>
    <TH>Date & Time</TH>
    <TH>Sample 1</TH>
    <TH>Sample 2</TH>
    <TH>Sample 3</TH>
  </TR>
  <TR>
    <TD>01/01/2003 00:00</TD>
    <TD>0.001</TD>
    <TD>0.345</TD>
    <TD>0.763</TD>
  </TR>
  <TR>
    <TD>01/01/2003 00:30</TD>
    <TD>0.002</TD>
    <TD>0.348</TD>
    <TD>0.694</TD>
  </TR>
</TABLE>

I have created the table header row by getting the first row of data and puting 
it in a variable. Each sample/label is then written out in a <TH></TH> tag.

<xsl:template match="/report/performance/data">
    <!-- Get the column headers from the first data row. -->
    <xsl:variable name="columntitles" select="row[1]/sample" />
    <!-- Create the table header -->
    <TABLE border="1" cellspacing="2" cellpadding="1" width="100%">
        <TR class="reportheader">
            <TH>DateTime</TH>
            <xsl:for-each select="$columntitles">
                <TH><xsl:value-of select="label"/></TH>
            </xsl:for-each>
        </TR>

Now I need to loop through each row writing out the <datetime> value and each 
sample/displayval.  The problem I have is that the samples aren't always in the 
same  order in each row, so I need to select each sample in the same order that 
they appear in the header row.  This is where I am stuck :(   How can I select 
the samples in the same order in which they appear in the $columntitles 
variable?  I tried the following:

        <!-- Write out each row of samples -->
        <xsl:for-each select="row">
            <xsl:variable name="samples" select="sample"/>
            <TR>
                <TD><xsl:value-of select="datetime"/></TD>
                <xsl:for-each select="$columntitles">
                    <TD><xsl:value-of 
select="$samples[./label=$columntitles/label]/displayval"/></TD>
                </xsl:for-each>
            </TR>
        </xsl:for-each>

but I only got the value of the first sample in all columns.  Is there a way of 
looping through the $columntitles/labels and selecting the sample where the 
label matches  in the current row?

Many thanks,

Nick

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



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