Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 26th, 2007, 07:18 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to populate a table with xml data in xslt

hi

im a newbie as my name suggests and ive been trying to populate a table for a time table.

Code:
<timetable>
 <university universityName="nottingham University">
   <startDate>05-02-2007</startDate>
   <endDate>11-05-2007</endDate>
   <startWeek>124</startWeek>
   <endWeek>138</endWeek>
 </university>
 <campus campusName="Wisteria"/>
 <Semester semesterNumber="1"/>
 <student studentNumber="2321557">
   <name>
     <firstName>Molly</firstName>
     <middleName>Collwen</middleName>
     <lastName>Caldwell</lastName>
   </name>
   <registeredModules>
     <studentsModules>3021</studentsModules>
     <studentsModules>3052</studentsModules>
     <studentsModules>3994</studentsModules>
   </registeredModules>
 </student>
 <modules>
   <module Crn="x73236">
     <moduleCrn>73236</moduleCrn>
     <moduleCode>3021</moduleCode>  
     <moduleName>Enterprise Database Systems</moduleName>
     <moduleTutor>
       <title/>
       <firstName>Patricia</firstName>
       <middleName/>
       <lastName>Ward</lastName>
       </moduleTutor>
   </module>
 </modules>
 <startTime>
   <st ID="s1">09:00 AM</st>
   <st ID="s2">09:30 AM</st>
   <st ID="s3">10:00 AM</st>
   <st ID="s4">10:30 AM</st>
   <st ID="s5">11:00 AM</st>
   <st ID="s6">11:30 AM</st>
 </startTime>
 <endTime>
   <et ID="e1">09:29 AM</et>
   <et ID="e2">09:59 AM</et>
   <et ID="e3">10:29 AM</et>
   <et ID="e4">10:59 AM</et>
   <et ID="e5">11:29 AM</et>
   <et ID="e6">11:59 AM</et>
   <et ID="e7">12:29 PM</et>
   <et ID="e8">12:59 PM</et>
 </endTime>
 <building buildingName="COLLB">
   <buildingRoom roomId="collb1">C114</buildingRoom>
   <buildingRoom roomId="collb2">C120</buildingRoom>
   <buildingRoom roomId="collb3">C121</buildingRoom>
   <buildingRoom roomId="collb4">CG76</buildingRoom>
 </building>
 <week weekNumber="124">
   <day dayOfWeek="Monday">
     <allocation allocationCrn="83488">
       <registeredModule moduleRef="x73245"/>
       <class classNumber="C01" classType="Lecture"/>
       <assignedRoom buildingRef="collb1"/>
       <starts startTimeRef="s1"/>
       <ends endTimeRef="e4"/>
     </allocation>
     <allocation allocationCrn="83490">
       <registeredModule moduleRef="x73245"/>
       <class classNumber="L02" classType="Laboratory"/>
       <assignedRoom buildingRef="collb3"/>
       <starts startTimeRef="s4"/>
       <ends endTimeRef="e8"/>
     </allocation>
  </day>
  <day dayOfWeek="Tuesday"/>
  <day dayOfWeek="Wednesday"/>
  <day dayOfWeek="Thursday">
     <allocation allocationCrn="83476">
       <registeredModule moduleRef="x73236"/>
       <class classNumber="C01" classType="Lecture"/>
       <assignedRoom buildingRef="collb4"/>
       <starts startTimeRef="s5"/>
       <ends endTimeRef="e8"/>
     </allocation>
  </day>
  <day dayOfWeek="Friday">
     <allocation allocationCrn="83476">
       <registeredModule moduleRef="x73236"/>
       <class classNumber="L05" classType="Laboratory"/>
       <assignedRoom buildingRef="collb2"/>
       <starts startTimeRef="s3"/>
       <ends endTimeRef="e5"/>
     </allocation>
  </day>
  <day dayOfWeek="Saturday"/>
  <day dayOfWeek="Sunday"/>
 </week>
</timetable>
so thats my basic gyst. there is obviously more than one week in a university calendar etc etc. now i need to make the weekly information in to a readable time table with the days on the x-axis as headers and the time on the y-axis. the university day starts at 9am and ends at 9pm (im only showing the abbreviated version). ive managed to get the header of the time table, and the times. but im so hopelessly lost with the populating of the table. PLEASE HELP. ive tried tutorials but and i understand them but i have no idea how to apply the functions etc. the xslt is shown below.

Code:
<xsl:template match="/">
 <html>
   <head>
     <title>Student TimeTable</title>
     <xsl:call-template name="style"/>
   </head>
   <body>
     <div id="wrapper">
       <table border="1">
         <xsl:apply-templates select="timetable"/>
       </table>
     </div>
   </body>
  </html>
</xsl:template>

<xsl:template name="style">
 <style type="text/css">
   <xsl:comment>
     a:hover { background: #9fc; }
     .header { background: #ccc; }
     .activity { background: #ff9; }
     .small { font-size: 70%; }
     .link { font-size: 120%; font-family: sans-serif; }
     .note{font-size: x-small; font-weight:bold;}
     a{text-decoration:none;}
     #wrapper {width:1200px;height:700px;}
   </xsl:comment>
 </style>
</xsl:template>

<xsl:template match="timetable">
 <thead>
   <tr>
     <td colspan="3">Student Timetable</td>
     <td colspan="4"><xsl:apply-templates select="student"/></td>
     <td><xsl:apply-templates select="student/name"/></td>
   </tr>
   <tr>
     <td colspan="8" align="right"></td>            
   </tr>
   <tr>
     <td colspan="8">
       <p>Any modules shown at the bottom of the page with a
       zero after it is not timetabled.</p>
     </td>
   </tr>
   <tr><td colspan="8"/></tr>
   <tr>
     <td colspan="8">
    <p>The current week is displayed by default. If this is not 
        a teaching week you will need to click on Next Week or 
        Previous Week to view the timetable.</p>
     </td>
   </tr>
   <tr>
     <td colspan="8">
    Go to (DD/MM/YYYY):<input type="text" name="date"/> 
                           <input type="submit" value="Submit"/>
     </td>
   </tr>
   <tr>
     <td colspan="2"><a href="">Previous Week</a></td>
     <td colspan="4">Week Of 07 May 2007 (137 of 138)</td>
     <td colspan="2" align="right"><a href="">Next Week</a></td>
   </tr>
   <tr>
     <th class="header"/>
     <xsl:apply-templates select="week[1]/day" mode="header"/>
   </tr>
 </thead>
 <tfoot>
   <xsl:apply-templates select= "student/registeredModules/studentsModules"/>
 </tfoot>
 <tbody>
   <xsl:apply-templates select ="startTime/st"/>
 </tbody>
</xsl:template>

<xsl:template match="studentsModules">
 <tr>
   <td><a href=""><xsl:value-of select="text()"/>- 0</a></td>
<td>- <xsl:value-of select="../../../modules/module/moduleCrn"/></td>
   <td>TBA</td>
 </tr>
</xsl:template>

<xsl:template match="student">
  <xsl:value-of select="@studentNumber"/>
</xsl:template>

<xsl:template match="name">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="day" mode="header">
 <th class="header">
   <xsl:value-of select="@dayOfWeek"/>
 </th>
</xsl:template>

 <xsl:template match="st">
   <xsl:if test="position( ) mod 2 > 0">
     <tr class="header">
       <td><xsl:value-of select="text()"/></td>
     </tr>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>
please help. im baffled and flumoxed :D

Reply With Quote
  #2 (permalink)  
Old May 27th, 2007, 05:13 AM
mhkay's Avatar
Wrox Author
Points: 18,274, Level: 58
Points: 18,274, Level: 58 Points: 18,274, Level: 58 Points: 18,274, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,932
Thanks: 0
Thanked 282 Times in 277 Posts
Default

Could you please try and produce a simplified version of the problem that captures the essence of where you are having difficulty? There's so much data here that's essentially irrelevant to the problem, and most of the code you've shown seems to be playing around the edges of it rather than attacking the core. You can add the frills later once you've got the basic structure right.

Showing the output you want to produce is always useful.

A couple of comments on your data: you should really be using ISO representations of dates and times; and your use of synthetic identifiers for each start and end time looks like relational normalization gone mad.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old May 27th, 2007, 04:16 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok.

i actually shortened all the code so il just show the simplified version of the XML and what i have done so far with the XSLT. i have no idea how to add the output i want to produce. ive never really used a forumn before. but basically i have to show a single week of timetable from monday to sunday and the corresponding lessons for each day.

what is iso representation?? sorry if thats a dumb question!!!! :)



Reply With Quote
  #4 (permalink)  
Old May 27th, 2007, 05:13 PM
mhkay's Avatar
Wrox Author
Points: 18,274, Level: 58
Points: 18,274, Level: 58 Points: 18,274, Level: 58 Points: 18,274, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,932
Thanks: 0
Thanked 282 Times in 277 Posts
Default

ISO representation of dates is in the form YYYY-MM-DD, and times are HH:MM:SS. It's useful to use this format partly because it's an international standard, and partly because it's directly supported by the XSLT 2.0/XPath 2.0 function library when you need to compare, sort, format, or do computations on dates and times.

I'm afraid that "i have to show a single week of timetable from monday to sunday and the corresponding lessons for each day" doesn't give me enough to work with. A timetable for a course? For a student? For a room? What are the rows, what are the columns, and what goes in the cells?

Your input format uses time spans like:

       <starts startTimeRef="s3"/>
       <ends endTimeRef="e5"/>

One way to tackle this is to ignore the actual times that this represents and just translate it into a block that covers period 3, 4, and 5. In XSLT 2.0 that's

<xsl:variable name="periods" select="xs:integer(substring(starts/@startTimeRef,2,1)) to xs:integer(substring(ends/@endTimeRef,2,1))"/>

When you're writing the cell for period $N on a Monday, you can then enter a particular allocation $a in the cell if $a/../@dayOfWeek = 'Monday' and $a/(xs:integer(substring(starts/@startTimeRef,2,1)) to xs:integer(substring(ends/@endTimeRef,2,1))) = $N. (Remember that "=" tests for membership of a set).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old May 27th, 2007, 06:39 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ah. okay. sorry for the silly questions. ok figured out a way to let you see what i would like the format to be like (took long enough)


http://www.geocities.com/mandinema/t...?1180305361105


thats all.

i saw what you meant by it would be helpful to see the intended output.

thanks again for the prompt replies...they are super super helpful.
mandi


Reply With Quote
  #6 (permalink)  
Old May 27th, 2007, 07:02 PM
mhkay's Avatar
Wrox Author
Points: 18,274, Level: 58
Points: 18,274, Level: 58 Points: 18,274, Level: 58 Points: 18,274, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,932
Thanks: 0
Thanked 282 Times in 277 Posts
Default

I'm no HTML expert so I have to guess what the HTML code for that looks like: presumably what you have to do is along the lines

for-each time-slot
  <tr>
     for-each day-of-the-week
          if there's a session starting at this time/day
          then <td rowspan="length of session">
               apply-templates select="session"
               </td>
          else if there's a session continuing at this time./day
          then ()
          else <td/>
  </tr>

which seems to translate into code something like the following. Im going to assume for simplification that the start and end times are labelled by numbers 1,2,3 rather than strings s1,s2, e1, e2:

<xsl:for-each select="startTime/st">
  <xsl:variable name="st" select="."/>
  <tr>
    <td><xsl:value-of select="."/></td>
    <xsl:for-each select="week[1]/day">
    <xsl:choose>
      <xsl:when test="allocation/starts[@startTimeRef=$st]">
         <xsl:variable name="alloc" select="allocation/starts[@startTimeRef=$st]"/>
         <xsl:variable name="length" select="$alloc/ends/@endTimeRef - $alloc/starts/@startTimeRef + 1"/>
         <td rowspan="{$length}">
            <xsl:apply-templates select="$alloc"/>
         </td>
       </xsl:when>
       <xsl:when test="alllocation[starts/@startTimeRef lt $st and ends/@endTimeRef ge $st]"/>
       <xsl:otherwise><td/></xsl:otherwise>
     </xsl:choose>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #7 (permalink)  
Old May 28th, 2007, 08:29 AM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi

was wondering, would it be better if i put the time allocations into the day and then assign each module to a specific starting time like so....

<week weekNumber="124">
  <day dayOfWeek="Monday">
    <timeOfDay start="8" time="12:30 AM"/>
    <timeOfDay start="9" time="1:00 PM">
      <allocated allocationRef="bis83488"/>
    </timeOfDay>
    <timeOfDay start="10" time="1:30 PM"/>
  </day>
</week>

it makes sense right if i was making a tree diagram that a week has days, and days has times??

next question:D.....

should i assign attributes that act like boolean values to the times that have module allocations ie

<week weekNumber="124">
 <day dayOfWeek="Monday">
  <timeOfDay st="1" time="12:00 PM"
  <timeOfDay st="2" time="12:30 PM"/>
  <timeOfDay st="3" time="1:00 PM">
   <allocated allocationRef="bis83488" starts="True" ends="False"/>
  </timeOfDay>
  <timeOfDay start="4" time="1:30 PM"/>
  <timeOfDay start="5" time="2:00 PM"/>
  <timeOfDay start="6" time="2:30 PM">
   <allocated allocationRef="bis83488" starts="False" ends="True"/>
  </timeOfDay>
  <timeOfDay start="7" time="3:00 PM"/>
  <timeOfDay start="8" time="3:30 PM">
   <allocated allocationRef="bis83480" starts="True" ends="False"/>
  </timeOfDay>
  <timeOfDay start="9" time="4:00 PM"/>
  <timeOfDay start="10" time="4:30 PM"/>
   <allocated allocationRef="bis83480" starts="False" ends="True"/>
  </timeOfDay>
 </day>
</week>

and then find some way to say that:

for-each time-slot
<tr>
 for-each day-of-the-week
  if there's a session starting at this time/day where starts is True
  then
   <td rowspan="length of session">
    apply-templates select="session"
   </td>
   else <td/>
  </tr>

hope that made sense!!!:)

mandi

Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
populate a drop list fro a data table greenbriar ADO.NET 2 July 22nd, 2008 11:18 PM
Using XSLT to convert XML to a table ? nobitavn94 XSLT 3 October 30th, 2006 11:03 AM
Auto-populate table data into form jeff394 Access VBA 2 April 19th, 2006 03:55 PM
constructing a HTML table from xml data using xslt rameshnarayan XSLT 0 September 19th, 2005 07:53 AM
populate data from Access Table into datagrid bisigreat VB How-To 6 August 17th, 2004 07:58 AM



All times are GMT -4. The time now is 12:03 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.