Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > ^.~ HELP!!!!! XML translation into HTML tables using XSL

Thread Tools

^.~ HELP!!!!! XML translation into HTML tables using XSL

Thuan Seah
Posts: n/a
Hi ^.^
Having problems formatting my xml document into a timetable(into html
using xsl) with days on x axis and time on y asix. My xml structure is
as follows:


I tried to use for-each to format but no matter how i loop though, if
i have two class on the same time but on different day(10:00 tuesday
and 10:00 thursday) would have two rows of 10:00 showing up with the
two classes.

As people who have hd a timetable before would know its repetitive and
does not form a regular timetable.


Reply With Quote
Martin Boehm
Posts: n/a
Thuan Seah wrote:

Hi Thuan,

I rewrote your XML Input a little, as you need a grid of some sorts to
make up a at least a skeleton of a timetable:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="timetable.xsl"?>
<day id="mo">Monday</day>
<day id="tu">Tuesday</day>
<day id="we">Wednesday</day>
<day id="th">Thursday</day>
<day id="fr">Friday</day>
<time id="8">0800</time>
<time id="10">1000</time>
<time id="12">1200</time>
<time id="14">1400</time>
<time id="16">1600</time>
<TimetableItem day="th" time="12">
<TimetableItem day="mo" time="12">

The TimeTableItem changed obviously, now having attributes making it
more easy to filter the right items for each grid-cell of the resulting

This is the XSL file I wrote, using nested for-each loops to generate
the HTML table:

<?xml version="1.0"?>

<xsl:stylesheet xmlnssl=""
<xslutput method="html">

<xsl:template match="/timetable">
<table border="1">
<!-- The   represents the &nbsp; which is not defined in XML
an en ENTITY, but is needed by IE to render empty td's
correctly -->
<!-- making the table header row -->
<td> </td>
<xsl:for-each select="grid/week/day">
<td><xsl:value-of select="."/></td>
<!-- making the the content -->
<xsl:for-each select="grid/day/time">
<xsl:variable name="time" select="@id"/>
<td><xsl:value-of select="."/></td>
<xsl:for-each select="../../week/day">
<xsl:variable name="day" select="@id"/>
<xsl:when test="count(/timetable/TimetableItem[@day=$day and
@time=$time]) != 0">
<xsl:apply-templates select="/timetable/TimetableItem[@day=$day
and @time=$time]"/>
  <!-- if there is nothing else, the &nbsp; must be
written -->

<xsl:template match="TimetableItem">
<div><strong><xsl:value-of select="CourseID"/></strong></div>
<div><xsl:value-of select="Lesson"/></div>
<xsl:value-of select="Location/Building"/>,
<xsl:value-of select="Location/Room"/>


The XPath Expression
/timetable/TimetableItem[@day=$day and @time=$time]
selects exactly the one node for the day/time in question, or nothing.

If there is nothing for the specific day/time, an non-breaking space is
needed (at least for IE) to display the empty table cell. I used the
count function to test for that, but maybe there is a more efficient
way, as in my solution the same XPath expression is executed twice in
case there is at least one course.

But this is academic, as for such a small solution there is no
performance impact one should worry about.

I hope I could help you,


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
using css in html embedded in xml rendered using xsl with javascript (phew) confused XML 3 05-25-2007 11:06 PM
XML and XSL translation David ASP .Net 6 02-25-2007 10:40 PM
xml charset translation with xsl XML 2 05-24-2006 08:54 PM
XSL Question tp xsl:for-each and xsl:variable XML 1 05-27-2005 09:25 PM
Is remote XML/XSL Translation possible? Gene Ellis XML 5 04-26-2004 07:11 PM