Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
Thuan Seah
Guest
Posts: n/a
 
      09-10-2003
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:

<TimetableItem>
<CourseID>Math1005</CourseID>
<Lesson>Lecture</Lesson>
<Location>
<Building>Copland</Building>
<Room>Theatre</Room>
</Location>
<Day>Tuesday</Day>
<Time>1200</Time>
</TimetableItem>

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.



HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

~Moon
 
Reply With Quote
 
 
 
 
Martin Boehm
Guest
Posts: n/a
 
      09-12-2003
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:

---timetable.xml---------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="timetable.xsl"?>
<timetable>
<grid>
<week>
<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>
</week>
<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>
</day>
</grid>
<TimetableItem day="th" time="12">
<CourseID>Math1005</CourseID>
<Lesson>Lecture</Lesson>
<Location>
<Building>Copland</Building>
<Room>Theatre</Room>
</Location>
</TimetableItem>
<TimetableItem day="mo" time="12">
<CourseID>Bla</CourseID>
<Lesson>Lecture</Lesson>
<Location>
<Building>bldg1</Building>
<Room>room1</Room>
</Location>
</TimetableItem>
</timetable>
-------------------------------------------------------------------

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

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

---timetable.xsl---------------------------------------------------
<?xml version="1.0"?>

<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput method="html">

<xsl:template match="/timetable">
<html>
<head>
<title>Timetable</title>
</head>
<body>
<table border="1">
<tr>
<!-- 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>
</xsl:for-each>
</tr>
<!-- making the the content -->
<xsl:for-each select="grid/day/time">
<xsl:variable name="time" select="@id"/>
<tr>
<td><xsl:value-of select="."/></td>
<xsl:for-each select="../../week/day">
<xsl:variable name="day" select="@id"/>
<td>
<xsl:choose>
<xsl:when test="count(/timetable/TimetableItem[@day=$day and
@time=$time]) != 0">
<xsl:apply-templates select="/timetable/TimetableItem[@day=$day
and @time=$time]"/>
</xsl:when>
<xsltherwise>
  <!-- if there is nothing else, the &nbsp; must be
written -->
</xsltherwise>
</xsl:choose>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

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

</xsl:stylesheet>
-------------------------------------------------------------------

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,

Martin


 
Reply With Quote
 
 
 
Reply

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 reynard.michel@gmail.com XML 2 05-24-2006 08:54 PM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Is remote XML/XSL Translation possible? Gene Ellis XML 5 04-26-2004 07:11 PM



Advertisments