converting xml to table width multiple columns by xsl

  • Thread starter =?iso-8859-1?q?Tobias_M=FCller?=
  • Start date
?

=?iso-8859-1?q?Tobias_M=FCller?=

Hello everybody,

I've got some weather data from my local wx station and want to display
this as a short table by using a XSL template. The data looks like

<?xml version="1.0"?>
<weather-data>
<station name="external temp." sensor="3" type="temperature"
unit="°C">
<datum date="04.09.2005" time="12:00" value="10" />
<datum date="04.09.2005" time="13:00" value="12" />
<datum date="04.09.2005" time="14:00" value="14" />
<datum date="04.09.2005" time="15:00" value="13.2" />
<datum date="04.09.2005" time="16:00" value="11.3" />
<datum date="04.09.2005" time="17:00" value="9.8" />
<datum date="04.09.2005" time="18:00" value="8.6" />
<datum date="04.09.2005" time="19:00" value="6.4" />
<datum date="04.09.2005" time="20:00" value="4" />
</station>
<station name="air pressure" sensor="3" type="pressure"
unit="mBar">
<datum date="04.09.2005" time="12:00" value="1020" />
<datum date="04.09.2005" time="12:30" value="1020" />
<datum date="04.09.2005" time="13:00" value="1021" />
<datum date="04.09.2005" time="13:30" value="1021" />
<datum date="04.09.2005" time="14:00" value="1022" />
<datum date="04.09.2005" time="15:00" value="1022" />
<datum date="04.09.2005" time="16:00" value="1023" />
<datum date="04.09.2005" time="17:00" value="1024" />
<datum date="04.09.2005" time="18:00" value="1025" />
<datum date="04.09.2005" time="19:00" value="1025" />
<datum date="04.09.2005" time="20:00" value="1025" />
</station>
</weather-data>

and should be converted in a table like

<table border="1">
<tr>
<th>date</th>
<th>time</th>
<th>external temp.</th>
<th>air pressure</th>
</tr>
<tr>
<td>04.09.2005</td>
<td>12:00</td>
<td>10</td>
<td>1020</td>
</tr>
<tr>
<td>04.09.2005</td>
<td>12:30</td>
<td></td>
<td>1020</td>
</tr>
<tr>
<td>04.09.2005</td>
<td>13:00</td>
<td>12</td>
<td>1021</td>
</tr>
...
</table>

And don't have any ideas to achieve this. I just got 2 seperate tables
using a for-each template, but I think this won't work here. Any ideas?

If there's no solution I could consider changing the way the XML file
looks like, but this way it's very easy to generate it.

Regards
Tobias
 
K

KemperR

Hello Tobias,

I think this is a common problem. I have made a quick and dirty BASIC
solution.
Please have a look into the code below.
One comment:
When I started with xslt I also use the fore-each a lot. But hownestly
saying in most cases it pushed me into bad or even wrong code. In case
you use templeates (may be with mode) you will probably have better
long term.

Hope that helped

Rolf

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:template match="/">
<html>
<body>
<table>
<tr>
<th>date</th>
<th>time</th>
<th>temp</th>
<th>pressure</th>
</tr>
<xsl:apply-templates
select="weather-data/station[@type='temperature']/datum"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="datum">
<xsl:variable name="vRefDate" select="@date"/>
<xsl:variable name="vRefTime" select="@time"/>

<tr>
<td>
<xsl:value-of select="$vRefDate" />
</td>
<td>
<xsl:value-of select="@vRefTime" />
</td>
<td>
<xsl:value-of select="@value" />
</td>
<td>
<xsl:value-of
select="/weather-data/station[@type='pressure']/datum[@date=$vRefDate
and @time=$vRefTime]/@value" />
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
 
D

dingbat

Tobias said:
I've got some weather data from my local wx station and want to display
this as a short table by using a XSL template.


Try this (and store the XML dates in a y-m-d format so they're
sortable - the W3C format is good)

<xsl:key name="readings-temperature"
match="/weather-data/station [@type = 'temperature'] /datum"
use="@time" />
<xsl:key name="readings-pressure"
match="/weather-data/station [@type = 'pressure'] /datum"
use="@time" />


<xsl:template name="weather-data" >


<!-- Get a set of readings
The full set, whatever took the reading
-->
<xsl:variable name="readings"
select="/weather-data/station/datum
[not((./@date = preceding::*/@date) and (./@time =
preceding::*/@time))]" />


<table border="1" >
<tr>
<th>date</th>
<th>time</th>
<th>external temp.</th>
<th>air pressure</th>
</tr>

<xsl:for-each select="$readings" >
<xsl:sort select="./@date" />
<xsl:sort select="./@time" />

<tr>
<td><xsl:value-of select="./@date" /></td>
<td><xsl:value-of select="./@time" /></td>
<td><xsl:value-of select="key ('readings-temperature', string
(./@time) )/@value" /></td>
<td><xsl:value-of select="key ('readings-pressure', string
(./@time) )/@value" /></td>
</tr>

</xsl:for-each>
</table>
</xsl:template>
 
?

=?iso-8859-1?q?Tobias_M=FCller?=

Thanks for your reply.

Rolfs version didn't show the entries where only a pressure was
measured and your version works correct, except the hardcoding of the
th-colums is a thorn in my side, because there could be more stations.
I'll try to declare to keys by a for-each of the stations.

Regards
Tobias

PS: The date in y-m-d is a good hint!
 
A

Andy Dingley

the hardcoding of the th-colums is a thorn in my side, because there could be more stations.

I don't claim to have post a solution, just a suggestion of some useful
techniques.

I see your point about multiple sensors, but that's a bigger question
and might well want some re-arranging of your data. That structure looks
very flattened and doesn't obviously express sensors that are co-located
at the same station.

Arrays of keys are troublesome in XSLT. Rather than trying to have a
keyset for each sensor, you might be better with a single large keyset
(or keysets for each measured property that's easily defined in
advance). Then treat the station / sensor ID as being like a
subclassification of the timestamp key value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top