473,378 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

converting xml to table width multiple columns by xsl

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

Sep 6 '05 #1
4 1970
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:output 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>

Sep 6 '05 #2
Tobias Müller wrote:
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>

Sep 6 '05 #3
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!

Sep 6 '05 #4
On 6 Sep 2005 13:52:16 -0700, "Tobias Müller" <ne**@twam.info> wrote:
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.

Sep 6 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

157
by: Dennis | last post by:
Is there some way --using, say, DOM or javascript-- to detect the current pixel width and/or height of a relatively sized table or of one of its columns or rows. I'm going to be writing javascript...
2
by: LC's No-Spam Newsreading account | last post by:
I have a form arranged in a table (you can see an example in the page http://cosmos.mi.iasf.cnr.it/~lssadmin/Website/LSS/Help/query.html) The table is on three columns but has a structure like...
61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
20
by: Andy Fish | last post by:
Hi All, I have a nice easy table and/or CSS formatting problem for any gurus out there. I have a table with 4 columns - no "width" attribute on anything. Some rows have 4 cells in (call...
6
by: Bill nguyen | last post by:
I use DatagridTableStyle to display data from table WF_Carrier_FreightChart. USers than can add/delete/edit rows in the Datagrid. Since the Datagrid's datasource "dCarrierFreight" is just a...
117
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
76
MMcCarthy
by: MMcCarthy | last post by:
Normalisation is the term used to describe how you break a file down into tables to create a database. There are 3 or 4 major steps involved known as 1NF (First Normal Form), 2NF (Second Normal...
25
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have...
2
by: bgold12 | last post by:
I have a problem that can be simplified to the following: I have a table with three columns. I need the middle column to be fixed width, let's say 100px. The other two columns I want to be variable...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.