Link to home
Start Free TrialLog in
Avatar of itortu
itortuFlag for United States of America

asked on

grid format xml

hello everybody,

I am working on converting an xml file into html output. Yesterday I got a lot of help for a similar task, but with a smaller xml file.
I need some help to format the this xml file.

I know which tags, and attributes I need. I just don't know how to access them and formatting them the way I need them, I was wondering if I could get some help on this, whatever help is good and always very much valuable.

this is my xml file:
http://wxpsta.myweather.net/wxpsta/xhour/long/90089.xml

i need to pull

hour header i need the hour and the attribute show
the obs type="temperature"
the obs type="feelslike"
the obs type="windspeed"
the obs type="winddirection"
the obs type="dewpoint"
the obs type="skyconditions"
the obs type="humidity"

this is how I need it to look like: (the grid at the middle of the page)
http://knbc.nbcweatherplus.com/wxpdetails/wxp002/detail.html?qs=;tab=hourly;token=undefined

I understand this is asking too much, I don't want to upset anybody, I thank for any help sincerely.


itortu.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Hi itortu,

this ain't too difficult
you can start your stylesheet like this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:apply-templates select="//hourly"/>
    </xsl:template>
</xsl:stylesheet>

this way the processor starts with the hourly element
then you can have a template for "obs" to create a row and a template for "ob" to have a cell
and that will get you started with the basics,
like this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <table border="1">
            <xsl:apply-templates select="//hourly"/>
        </table>
    </xsl:template>
    <xsl:template match="obs">
        <tr>
            <th><xsl:value-of select="@type"/></th>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="ob">
        <td><xsl:value-of select="."/></td>
    </xsl:template>
</xsl:stylesheet>

Cheers!
itortu,

here is a next iteration
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <table border="1">
            <xsl:apply-templates select="//hourly"/>
        </table>
    </xsl:template>
    <xsl:template match="hourheader">
        <th>hour</th>
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="obs">
        <tr>
            <th><xsl:value-of select="@type"/></th>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="ob">
        <td><xsl:value-of select="."/></td>
    </xsl:template>
    <xsl:template match="hour">
        <td><xsl:value-of select="."/></td>
    </xsl:template>
    <xsl:template match="icon">
        <td><xsl:value-of select="@type"/></td>
    </xsl:template>
</xsl:stylesheet>

now you need to start being selective (dropping the rows you don't need, with an if clause)
and start adding classes to the row and cell ellements
in order for your CSS to get a grip on the layout

but I think you can start doing that yourself

cheers

Geert
itortu,

here is for instance a finetuning for the hour title bar
(just the templates I changed
 
   <xsl:template match="hourheader">
        <tr id="hourheader" class="hour">
            <th>Hour</th>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="hour">
        <td  align="center" class="{@show}"><xsl:value-of select="."/></td>
    </xsl:template>
Avatar of itortu

ASKER

Oh this xslt is difficult topic. I want to understand it but I get confused so easily.
I wish there was an easier way to access each of the elements I need something more explicit.
change <xsl:value-of select="."/>
to
 <xsl:value-of select="hour"/> or whatever element needed.

I am not complaining about your work, not at all. I am just overwhelmed by my task. I really appreciate your help Gertone.

One comment, by using your file, I see that the hour is repeated twice inside the grid, why is that?

Hour       7a       8a       9a       10a       11a       12p       1p       2p       3p       4p       5p       6p       7p       8p       9p       10p       11p       12a       1a       2a       3a       4a       5a       6a
temperature       59       60       62       64       65       66       73       73       72       71       69       67       66       62       61       61       60       59       58       57       57       57       56       56
feelslike       59       60       62       64       65       66       73       73       72       71       69       67       66       62       61       61       60       59       58       57       57       57       56       56
windspeed       2       2       1       1       2       4       7       9       10       10       8       7       6       4       2       1       1       1       1       1       2       3       4       4
dewpoint       57       56       53       49       45       42       46       45       46       47       48       48       50       51       50       50       51       51       52       52       52       52       51       51
humidity       93       89       73       59       51       43       39       38       40       43       47       52       57       63       66       68       70       74       78       80       81       82       85       87
winddirection       s       se       se       s       sw       sw       sw       sw       sw       sw       sw       sw       sw       sw       sw       sw       sw       sw       sw       w       w       w       w       w
skyconditions       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearnight       clearnight       partlycloudynight       partlycloudynight       partlycloudynight       partlycloudynight       partlycloudynight       partlycloudynight       partlycloudynight       partlycloudynight       partlycloudynight       partlycloudynight
precipitation                                                                                                                                                 
precipamount       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
preciprain       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
precipsnow       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
houricon       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearday       clearnight       clearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight       mostlyclearnight
Hour       7a       8a       9a       10a       11a       12p       1p       2p       3p       4p       5p       6p       7p       8p       9p       10p       11p       12a
temperature       56       59       63       66       68       70       74       74       74       72       70       67       64       61       59       58       58       56
feelslike       56       59       63       66       68       70       74       74       74       72       70       67       64       61       59       58       58       56
windspeed       2       2       3       3       4       5       6       8       9       10       9       8       6       4       3       2       1       1
dewpoint       49       49       47       46       46       46       47       48       48       48       48       46       48       48       49       49       49       50
humidity       76       66       57       49       45       41       38       37       38       41       45       48       53       61       66       70       74       77
winddirection       w       sw       s       s       sw       sw       sw       sw       sw       sw       sw       sw       sw       sw       s       s       s       sw
skyconditions       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       partlycloudyday       clearnight       clearnight       clearnight       clearnight       clearnight       clearnight
precipitation                                                                                                             
precipamount       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
preciprain       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
precipsnow       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
houricon       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       mostlyclearday       clearnight       clearnight       clearnight       clearnight       clearnight       clearnight
Avatar of itortu

ASKER

hi Gertone, sorry for the long post, but I am a lost.

for the icons, I need to display a picture related to the value of the icon type. I was thinking doing this byt creating an attribute. but using the technique that you offer, I don't know how can I separate all the icons, from only the ones that i need.
you suggested:

 <xsl:template match="icon">
        <td>
            <xsl:value-of select="@type"/>
        </td>
</xsl:template>

and I was thinking something like this:

<img>
    <xsl:attribute name="src">http://www.weathericons.net/wx/icons/cc/40x40/<xsl:value-of select="icon-select"/>.gif</xsl:attribute>
</img>

how can these two ideas be merged?

thanks!
Avatar of itortu

ASKER

Hi Gertone, are u still around? I hope so.
i just realized that there are forecast for two days, the current day, and the nextday.
how can i just select all only the current day elements?

Thank you,
itortu,
> Hi Gertone, are u still around? I hope so

I was travelling home from work the past hour
I am looking at it again
I have some answers if you have some time
itortu,
> i just realized that there are forecast for two days, the current day,

assuming this is the first day in the row,
this will work

    <xsl:template match="/">
        <table border="1">
            <xsl:apply-templates select="//day[1]//hourly"/>
        </table>
    </xsl:template>
itortu,
> I see that the hour is repeated twice inside the grid, why is that?

that is solved now I guess
itortu,
> change <xsl:value-of select="."/>
> to
>  <xsl:value-of select="hour"/> or whatever element needed.

well, you can be very explicit about this

    <xsl:template match="hourly">
        <xsl:apply-templates select="hourheader"/>
        <xsl:apply-templates select="obs[@type='temperature']"/>
        <xsl:apply-templates select="obs[@type='feelslike']"/>
        <xsl:apply-templates select="obs[@type='windspeed']"/>
        <xsl:apply-templates select="obs[@type='winddirection']"/>
        <xsl:apply-templates select="obs[@type='dewpoint']"/>
        <xsl:apply-templates select="obs[@type='skyconditions']"/>
        <xsl:apply-templates select="obs[@type='humidity']"/>
     </xsl:template>
itortu,
> how can these two ideas be merged

add this template

    <xsl:template match="icon[../@type='winddirection']">
        <td align="center" class="winddirection">
            <img src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_{@type}.gif" alt="" />
        </td>
    </xsl:template>
itortu,

in case you don't know...
the {} in the previous post are attribute value templates
they are a bit like xsl:value-of in a context where that is not allowed, such as in attribute values,
as in the example above
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itortu

ASKER

I am working on it. I think I will spend most of today doing this, tomorrow I will let you know how did it go.

By now, thank you so much for your help Geert!

itortu
Avatar of itortu

ASKER

Hi Geert!

after spending a lot of time on this I finally got it to work the way I needed it.

this is how my xsl look in the end:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="myweatherpage">

    <table class="pfp">
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="forecast">
<xsl:for-each select="//day[position() = 1]">
    <table border="0" cellspacing="0">
      <tr>
          <td>
              <p class="date"><xsl:value-of select="date"/></p><xsl:value-of select="location/@city"/>
          </td>
      </tr>
    </table>
      
<xsl:apply-templates select="hourly"/>            
    </xsl:for-each>
        <table border="0" cellspacing="0" width="460"></table>      
</xsl:template>


<xsl:template match="obs">
      <tr>
            <td class="tablelabel">
            <p class="tablelabel" align="right">
            <xsl:choose>
            <xsl:when test="@type='temperature'">
                  Temp</xsl:when>
            <xsl:when test="@type='windspeed'">
                  Winds</xsl:when>
            <xsl:when test="@type='winddirection'">
                  Direction</xsl:when>
            <xsl:when test="@type='precipitation'">
                  Precip</xsl:when>
            <xsl:when test="@type='feelslike'">
                  Feels Like</xsl:when>
            <xsl:when test="@type='windchill'">
                  Wind Chill</xsl:when>
            <xsl:when test="@type='heatindex'">
                  Heat Index</xsl:when>
            <xsl:when test="@type='dewpoint'">
                  Dew Point<xsl:text></xsl:text></xsl:when>
            <xsl:when test="@type='skyconditions'">
                  Skies</xsl:when>
            <xsl:when test="@type='humidity'">
                  <xsl:text>Humidity</xsl:text></xsl:when>
            </xsl:choose>
            </p></td>
            <xsl:for-each select="ob|icon">
                  <xsl:apply-templates select="."/>
            </xsl:for-each>
    </tr>
</xsl:template>      

<xsl:template match="hourly">

<TABLE border="0" cellspacing="0" cellpadding="0">
    <TR>
        <TD>
          <TABLE align="center" border="0" cellspacing="0" width="100%">
              <tr>
                  <td class="forecastperiod">
                    <p class="forecastperiod">Hour by hour</p>
                </td>
            </tr>
            <tr>
                <td class="tablelabel">
                    <p class="tablelabel"><xsl:value-of select="label"/></p>
                </td>
            
                    <TABLE align="center" class="hourly" border="0" cellspacing="1" width="460">
                        <xsl:apply-templates select="hourheader"/>
                        <xsl:apply-templates select="obs[@type='temperature']"/>
                        <xsl:apply-templates select="obs[@type='feelslike']"/>
                        <xsl:apply-templates select="obs[@type='windspeed']"/>
                        <xsl:apply-templates select="obs[@type='winddirection']"/>
                        <xsl:apply-templates select="obs[@type='dewpoint']"/>
                        <xsl:apply-templates select="obs[@type='skyconditions']"/>
                        <xsl:apply-templates select="obs[@type='precipitation']"/>
                        <xsl:apply-templates select="obs[@type='humidity']"/>
                    </TABLE>
              </tr>
          </TABLE>
        </TD>
    </TR>
</TABLE>
</xsl:template>

<xsl:template match="ob">
    <xsl:choose>
        <xsl:when test="@type='max'">
            <td class="maxvalue" width="15">
                <p class="tablevaluebig" align="center"><xsl:value-of select="."/></p>
            </td>
      </xsl:when>
      <xsl:when test="@type='min'">
            <td class="minvalue" width="15">
                <p class="tablevaluebig" align="center"><xsl:value-of select="."/></p>
            </td>
      </xsl:when><xsl:otherwise>
            <td class="tablevalue" width="15">
                <p class="tablevalue"><xsl:value-of select="."/></p>
            </td>
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>      

<xsl:template match="icon">
      <td width="30" class="tableicon">
          <p class="tablevalue" align="center">
      <xsl:choose>
      <xsl:when test="@type='clearday'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/clearday.gif" alt="Clear skies"/>
      </xsl:when>
      <xsl:when test="@type='clearnight'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/clearnight.gif" alt="Clear skies"/>
      </xsl:when>
      <xsl:when test="@type='cloudyday'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/cloudyday.gif" alt="Cloudy"/>
      </xsl:when>
      <xsl:when test="@type='cloudynight'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/cloudynight.gif" alt="Cloudy"/>
      </xsl:when>
      <xsl:when test="@type='mostlycloudyday'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/mostlycloudyday.gif" alt="Mostly cloudy"/>
      </xsl:when>
      <xsl:when test="@type='mostlycloudynight'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/mostlycloudynight.gif" alt="Mostly cloudy"/>
      </xsl:when>
      <xsl:when test="@type='partlycloudyday'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/partlycloudyday.gif" alt="Partly cloudy"/>
      </xsl:when>
      <xsl:when test="@type='partlycloudynight'">
            <IMG border="0" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/18x18/partlycloudynight.gif" alt="Partly cloudy"/>
      </xsl:when>
      <xsl:when test="@type='s'">
            <img src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_n.gif" alt="Winds from the south"/>
      </xsl:when>
      <xsl:when test="@type='sw'">
            <img  src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_ne.gif" alt="Winds from the southwest"/>
      </xsl:when>
      <xsl:when test="@type='w'">
            <img  src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_e.gif" alt="Winds from the west"/>
      </xsl:when>
      <xsl:when test="@type='nw'">
            <img  src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_se.gif" alt="Winds from the northwest"/>
      </xsl:when>
      <xsl:when test="@type='n'">
            <img  src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_s.gif" alt="Winds from the north"/>
      </xsl:when>
      <xsl:when test="@type='ne'">
            <img  src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_sw.gif" alt="Winds from the northeast"/>
      </xsl:when>
      <xsl:when test="@type='e'">
            <img  src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_w.gif" alt="Winds from the east"/>
      </xsl:when>
      <xsl:when test="@type='se'">
            <img  src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/arrow_nw.gif" alt="Winds from the southeast"/>
      </xsl:when>
      <xsl:when test="@type='mix'">
            <img width="16" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/precip/mix.gif" alt="Mixed precipitation expected"/>
      </xsl:when>
      <xsl:when test="@type='mixchance'">
            <img width="16" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/precip/mixchance.gif" alt="Chance of mixed precipitation"/>
      </xsl:when>
      <xsl:when test="@type='rainlikely'">
            <img width="16" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/precip/rain.gif" alt="Rain expected"/>
      </xsl:when>
      <xsl:when test="@type='rainchance'">
            <img width="16" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/precip/rainchance.gif" alt="Chance of rain"/>
      </xsl:when>
      <xsl:when test="@type='snowlikely'">
            <img width="16" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/18x18/snow.gif" alt="Snow expected"/>
      </xsl:when>
      <xsl:when test="@type='snowchance'">
            <img width="16" src="http://www.nbcweatherplus.com/sh/wxplus/icons/xhour/wind/18x18/snowchance.gif" alt="Chance of snow"/>
      </xsl:when>
      
      </xsl:choose>
      </p></td>
</xsl:template>            

<xsl:template match="hourheader">
    <tr>
        <td class="tablelabeltime">
          <p class="tablelabel">Hour</p>
      </td>
      <xsl:for-each select="hour">
          <xsl:apply-templates select="."/>
      </xsl:for-each>
    </tr>
</xsl:template>            

<xsl:template match="hour">
<xsl:if test="@show='light'">
    <td width="20" class="day">
        <p class="timeofday"><xsl:value-of select="."/></p>
    </td>
</xsl:if>

<xsl:if test="@show='dark'">
    <td width="20" class="night">
        <p class="timeofday"><xsl:value-of select="."/></p>
    </td>
</xsl:if>

</xsl:template>            

</xsl:stylesheet>

Avatar of itortu

ASKER

thankyou very much for your help. I wish I could say I like xslt but I just strugle so much to understand it. and it is so random the times I have been needing to use it.

anyways, I don't mean to take much of your time, just wanted to say thanks once againg.


Take care,

itortu.
itortu,
you are welcome
XSLT is quiet fun, once you get the hang of it...
in the beginning it can be a real real pain...
I hope you once get at the fun stage
cheers