Display Local Weather Data On Your Site
page 4 of 7
by Andrew Mooney
Feedback
Average Rating: 
Views (Total / Last 10 Days): 49822/ 107

Use XSL to Transform the XML Feed

Now that we have the XML feed, we need to create an XSL file to transform it into HTML so that we can display it on our web site. After we have decided which nodes to display from the XML feed we need to decide how to transform them with XSL.

Looking at the XSL in Listing 2 you will notice that a test is made to see if certain nodes exist before displaying them. The reason for this is that this example uses a default XML file that gets loaded if the XML feed is unavailable.

You may have noticed in the XML feed in Listing 1 that some of the nodes contained a value of "NA." The NWS is using this value to indicate that there is no currently applicable data for that node. Some examples of nodes that might contain NA values are heat index, wind chill, and wind gust. This NA value can be used to determine whether or not to display a certain node. For example, there are only heat index or wind chill values under certain conditions and sometimes there is neither. You do not want to display the label for heat index followed by NA. So, the example XSL checks to see if the wind chill node is NA before displaying it.

I have created an example XSL style sheet which is shown in Listing 2. Name the XSL file Weather.xsl and place it in the App_Data folder of your web application.

Listing 2 - This is the XSL file that we will use to transform the XML weather feed.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="html" />
<xsl:template match="/">
  <!-- This is an XSL template file. -->
  <table bgcolor="#eeeeee" cellspacing="0" cellpadding="1">
    <tr bgcolor="lightsteelblue">
      <td colspan="2">
        <xsl:value-of select="current_observation/location"/>
        <br/>
        <xsl:value-of select="current_observation/observation_time"/>
      </td>
    </tr>
    <xsl:if test="current_observation/weather != ''">
    <tr>
      <td>
        Weather:
      </td>
      <td>
        <xsl:value-of select="current_observation/weather"/>
      </td>
    </tr>
    </xsl:if>
    <xsl:if test="current_observation/temperature_string != ''">
    <tr>
      <td>
        Temperature:
      </td>
      <td>
        <xsl:value-of select="current_observation/temperature_string"/>
      </td>
    </tr>
    </xsl:if>
    <xsl:if test="current_observation/relative_humidity != ''">
    <tr>
      <td>
        Humidity:
      </td>
      <td>
        <xsl:value-of select="current_observation/relative_humidity"/> %
      </td>
    </tr>
    </xsl:if>
    <xsl:if test="current_observation/wind_string != ''">
    <tr>
      <td>
        Wind:
      </td>
      <td>
        <xsl:value-of select="current_observation/wind_string"/>
      </td>
    </tr>
    </xsl:if>
    <xsl:if test="current_observation/windchill_string != '' and 
    current_observation/windchill_string != 'NA'">
    <tr>
      <td>
        Wind Chill:
      </td>
      <td>
        <xsl:value-of select="current_observation/windchill_string"/>
      </td>
    </tr>
    </xsl:if>
    <xsl:if test="current_observation/heat_index_string != '' and 
    current_observation/heat_index_string != 'NA'">
    <tr>
      <td>
        Heat Index:
      </td>
      <td>
        <xsl:value-of select="current_observation/heat_index_string"/>
      </td>
    </tr>
    </xsl:if>   </table>
</xsl:template>
</xsl:stylesheet> 

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-02-27 4:02:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search