xsl-list
[Top] [All Lists]

Re: stylesheet for generating html table and aditionally paste calendar data

2005-10-02 13:11:31
Hi Lars,

I haven't seen a reply to this question, so I will try and give it a shot.

now i want to generate a big html table first wich contains all days of
the year with the name of the moths as table captions. like this:

| jan | feb| mar| apr| ... | dec|
----------------------------
 1      1     1     1    ...    1
 ...     ...    ...     ...   ...   ...
31    28   31    30   ...   31

(table completely filled). I dont know how to do that. Because, if there
are only 5 events per moth, the table nevertheless needs to be filled
with all numbers of the days.

I thought it would be stupid to create this table manually in the
stylesheet!

I would have the following XML in a file, lets call it Months.xml:
<!-- Filename = Months.xml -->
<?xml version="1.0">
<Months>
  <Month><name>jan</name><days>31</days></Month>
  <Month><name>feb</name><days>29</days></Month>
  ...
  <Month><name>dec</name><days>31</days></Month>
</Months>

Then I would be able to generate the html table using the following
simple XSLT (please insert the correct namespace for xslt - I can
never remember it):
<?xml version="1.0">
<xsl:stylesheet xmlns:xsl="...">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:variable name="st" select="document('')"/>
<table>
<tr> <!-- First get the months in 12 columnms (one row) -->
<xsl:for-each select="document('Months.xml')/Months/Month">
  <td><xsl:value-of select="name"/></td>
</xsl:for-each>
</tr>
<!-- Now loop over the dates row (Wendel Piez Method) -->
<xsl:for-each select="($st//node()| $st//@* | $st//namespace::*)
[position() &lt;= 31]"> <!-- Ensure that there are more than 31 items
to loop over -->
  <xsl:variable name="pos" select="position()"/> <!-- position of row -->
  <tr>  <!-- Make one row for each date -->
  <xsl:for-each select="document('Months.xml')/Months/Month">
    <td>  <!-- Now enter the column -->
    <xsl:choose>
      <xsl:when test="$pos&lt;=days"><xsl:value-of select="days"/></xsl:when>
      <xsl:otherwise>&#160;</xsl:otherwise>
    </xsl:choose>
    <!-- (*) See below -->
    </td>
  </xsl:for-each>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

Now, this should be enough for creating the table that you want.

the events should then be pasted behind a specific day accoring to the
.xml source. the second prob ist how can i insert the event at the
specific place in the table?

Well, to answer this question, you will have to enter additional code.
One solution is to enter the following where (*) is marked in the
stylesheet above:

<xsl:for-each 
select="/schedule/month[(_at_)n=position()]/day[(_at_)n=$pos]/date">
  <br/>Time: <xsl:value-of select="@time"/>
  <br/>Event: <xsl:value-of select="event"/>
</xsl:for-each>

This code assumes that schedule is the root element of your input xml.
It selects all date elements that have the same month and the same day
as the current tr and td being filled in the output.

If there are many events, it might be an idea to create a key to hold
the variables (this must be a top-level element in the stylesheet):
<xsl:key name="eventdate-by-month-and-day"
  match="date"
  use="concat(../../@n,'_',../@n)"/>

In this case, the code marked with the asterix (*) will be:
<!-- (Muenchian Method) -->
<xsl:for-each select="$eventdate-by-month-and-day"
use="concat(position(),'_',$pos)">
  <br/>Time: <xsl:value-of select="@time"/>
  <br/>Event: <xsl:value-of select="event"/>
</xsl:for-each>

I hope this helps you. You can, of course, change the output to suit
your solution.
Regards,
Ragulf Pickaxe :-)

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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