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

Transforming XML to HTML Table

Subject: Transforming XML to HTML Table
From: "Nick Foster" <Nick.Foster@xxxxxxxxxxx>
Date: Wed, 29 Jan 2003 12:00:03 -0000
transform xml to html table
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


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.