[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert xml weather data from fahrenheit to celsius

Posted on 2006-05-25
14
Medium Priority
?
631 Views
Last Modified: 2012-06-27
hello, i would like to knwo if someone has some experience in converting xml weather data that comes in fahrenheit to its equivalent in celsius.

The conversion formula is Tc = (5/9)*(Tf-32);

Tc = temperature in degrees Celsius, Tf = temperature in degrees Fahrenheit

Using the above formula, you would first subtract 32 from the Fahrenheit temperature and get 66.6. Then you multiply 66.6 by five-ninths and get 37 degrees Celsius.

I got a xslt template that displays the weather data, but currently only displays fahrenheit.
Could someone, if possible tech me how to do the conversion inside the xsl template, or if this is even possible?

Thank you so much!

here is the template:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="/current-conditions/reporting-station"/>
<xsl:if test="not(/seven-day-forecast/reporting-station)">
      <xsl:apply-templates select="/three-day-forecast/reporting-station"/>
</xsl:if>
<xsl:apply-templates select="/zone-forecast/reporting-station"/>
</xsl:template>

<xsl:template match="/three-day-forecast/reporting-station">
<three-day-forecast>
<table class="SectionTitle" border="0" width="100%">
    <tr>
        <td valign="top">3-Day Forecast</td>
        <td align="right"><script language="javascript" src="http://ads.myadmanager.com/adserver/per=contextual/id=hello"></script></td>
    </tr>
    <tr>
        <td colspan="2"><img src="Images/graydot.gif" border="0" height="1" width="100%" vspace="0"/></td>
    </tr>
</table>
<table class="FivedayForecast" border="0" width="100%">
<tr>
<xsl:apply-templates select="forecast-day[position()&lt;=7]"/>
</tr>
</table>
</three-day-forecast>
</xsl:template>

<xsl:template match="forecast-day">
<td class="Day">
    <table border="0">
        <tr>
            <td width="20%" class="Day"><xsl:value-of select="day"/></td>
        </tr>
        <tr>
            <td width="20%" class="Icon"><img src="{icon-name}"/></td>
        </tr>
        <tr>
            <td width="20%" class="Condition"><xsl:value-of select="sky-conditions"/></td>
        </tr>
        <tr>
            <td width="20%" class="Temp"><span class="High"><xsl:value-of select="high"/>&#176;</span> / <span class="Low"><xsl:value-of select="low"/>&#176;</span></td>
        </tr>
    </table>
</td>
</xsl:template>

<xsl:template match="/current-conditions/reporting-station">
<location>
<table>
    <tr>
        <td class="LocationTitle"><xsl:value-of select="city"/>, <xsl:value-of select="state"/></td>
    </tr>
</table>
</location>

<current-conditions>
<table class="SectionTitle" border="0" width="200">
    <tr>
        <td>Current Conditions<br/><img src="Images/graydot.gif" border="0" height="1" width="100%" vspace="0"/></td>
    </tr>
</table>
<table class="CurrentConditions" border="0" width="200">
    <tr>
        <td colspan="2" class="DateTime"><xsl:value-of select="../date"/>, <xsl:value-of select="../time"/></td>
    </tr>
    <tr>
        <td colspan="2" class="Label"><SPAN class="MediumData"><xsl:value-of select="sky-conditions"/></SPAN></td>
    </tr>
    <xsl:if test="temperature!=''">
        <tr>
            <td class="Label">Temperature</td>
            <td class="Data"><xsl:value-of select="temperature"/>&#176;</td>
        </tr>
    </xsl:if>
    <xsl:if test="dew-point!=''">
        <tr>
            <td class="Label">Dew Point</td>
            <td class="Data"><xsl:value-of select="dew-point"/>&#176;</td>
        </tr>
    </xsl:if>
    <xsl:if test="wind-chill!=''">
        <tr>
            <td class="Label">Wind Chill</td>
            <td class="Data"><xsl:value-of select="wind-chill"/>&#176;</td>
        </tr>
    </xsl:if>
    <xsl:if test="heat-index!=''">
        <tr>
            <td class="Label">Heat Index</td>
            <td class="Data"><xsl:value-of select="heat-index"/>&#176;</td>
        </tr>
    </xsl:if>
    <xsl:if test="relative-humidity!=''">
        <tr>
            <td class="Label">Humidity</td>
            <td class="Data"><xsl:value-of select="relative-humidity"/>%</td>
        </tr>
    </xsl:if>
    <xsl:if test="precip-today!=''">
        <tr>
            <td class="Label">Precip. Today</td>
            <td class="Data"><xsl:value-of select="precip-today"/> in.</td>
        </tr>
    </xsl:if>
    <xsl:if test="wind-direction!=''">
        <tr>
            <td class="Label">Wind</td>
            <td class="Data"><xsl:value-of select="wind-direction"/><xsl:if test="wind-speed!=''"> at <xsl:value-of select="wind-speed"/> mph</xsl:if></td>
        </tr>
    </xsl:if>
    <xsl:if test="altimeter!=''">
        <tr>
            <td class="Label">Barometer</td>
            <td class="Data"><xsl:value-of select="altimeter"/> in. <xsl:if test="pressure-tendency!=''"> <xsl:value-of select="pressure-tendency"/></xsl:if></td>
        </tr>
    </xsl:if>
    <xsl:if test="visibility!=''">
        <tr>
            <td class="Label">Visibility</td>
            <td class="Data"><xsl:value-of select="visibility"/> mi.</td>
        </tr>
    </xsl:if>
</table>
</current-conditions>
</xsl:template>
</xsl:stylesheet>
                                    
0
Comment
Question by:itortu
  • 10
  • 4
14 Comments
 
LVL 15

Expert Comment

by:mshogren
ID: 16764386
Assuming that the temperature is already numeric, you should be able to replace

<xsl:value-of select="temperature"/>

with

<xsl:value-of select="(5/9) * (number(temperature) - 32)"/>

0
 

Author Comment

by:itortu
ID: 16764454
hi mshogren, first of all thank you for your help.

i tried your suggestion and change this line:
       
        <xsl:if test="temperature!=''">
            <tr>
                <td class="mywx-Label">Temperature</td>
                <td class="mywx-Data"><xsl:value-of select="temperature"/>&#176;</td>
            </tr>
        </xsl:if>

to make it like this:

        <xsl:if test="temperature!=''">
            <tr>
                <td class="mywx-Label">Temperature</td>
                <td class="mywx-Data"><xsl:value-of select="(5/9) * (number(temperature) - 32)"/>&#176;</td>
           </tr>
       </xsl:if>

and when i run the page i got this error: error= 80004005 (Unspecified error) - msxml3.dll Expression does not return a DOM node. (-->5/<--9) * (number(temperature) - 32)

any ideas why?
0
 

Author Comment

by:itortu
ID: 16764478
actually the complete error is this:

Error retrieving XML data in default.asp
Error number: N/A
200 - HTML: COM error= 80004005 (Unspecified error) - msxml3.dll Expression does not return a DOM node. (-->5/<--9) * (number(temperature) - 32)


the asp page default seems to be expecting some type of node, but the formula is returning not a DOm node, can this be fixed without altering the .asp page?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:itortu
ID: 16764516
this is the xml file i am using:

&#8722;<three-day-forecast gmt="1148589705" gmt-offset="-5">
<date>Thursday, May 25</date>
<dateabbr>05/25/06</dateabbr>
<time>04:05 PM CDT</time>
&#8722;<reporting-station code="EDDI">
<city>Berlin</city>
<state>Germany</state>
&#8722;<forecast-day>
<day>Thursday</day>
<high>61</high>
<low>42</low>
<icon-select>10</icon-select>
<sky-conditions>Rain</sky-conditions>
</forecast-day>
&#8722;<forecast-day>
<day>Friday</day>
<high>60</high>
<low>43</low>
<icon-select>3</icon-select>
<sky-conditions>Partly cloudy</sky-conditions>
</forecast-day>
&#8722;<forecast-day>
<day>Saturday</day>
<high>63</high>
<low>42</low>
<icon-select>10</icon-select>
<sky-conditions>Rain</sky-conditions>
</forecast-day>
</reporting-station>
</three-day-forecast>
0
 

Author Comment

by:itortu
ID: 16764532
i think is not a problem with the asp page, i just made a simple page to load the xml and the xsl, and still got the same error.
0
 

Author Comment

by:itortu
ID: 16764801
i hope you someone can offer some help.

thanks
0
 

Author Comment

by:itortu
ID: 16764886
i think my problem is related to xpath. please some help : )
0
 
LVL 15

Accepted Solution

by:
mshogren earned 1200 total points
ID: 16764908
oops, I forgot the correct division operator for XSL.  Try

<xsl:value-of select="(5 div 9) * (number(temperature) - 32)"/>
0
 

Author Comment

by:itortu
ID: 16764961
ahhh, that worked very well!

you know something, after doing the conversion, the numbers in celsius are returned in this format: 12.222222222222223°

is there any function to round it up so it does not show the decimals?

thank you very much.
0
 

Author Comment

by:itortu
ID: 16765060
how can i use the format-number inside this part:

<xsl:value-of select="(5 div 9) * (number(wind-chill)- 32)"/>

?
0
 
LVL 15

Expert Comment

by:mshogren
ID: 16765123
You don't need to use format-number though you can if you like.  I would use round() like this:

<xsl:value-of select="round((5 div 9) * (number(wind-chill)- 32))"/>
0
 

Author Comment

by:itortu
ID: 16765272
ideally i would like to see the degree number in this format: 99
and not 99.99 neither 99.9999999

by using the round method, would then the decimal places willl be omitted?

thanks!

0
 
LVL 15

Expert Comment

by:mshogren
ID: 16765351
If you just want to remove the decimal places rather an round to the nearest integer use floor().  It shouldn't take long to try both out and see whihc one gives preferable results.

<xsl:value-of select="floor((5 div 9) * (number(wind-chill)- 32))"/>
0
 

Author Comment

by:itortu
ID: 16769533
Thank you very much mshogren!
I have another question posted, but I have not receive any comments yet, it is xml related, and if you have the time may be you can help me with my problem.

once again thank you very much for your help on this question!

here is the link to my other question:

http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21862991.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question