Pages

Thursday, November 18, 2010

Pull google weather and display in Delivery Server - Part 1

I have found two ways to display weather within delivery server. The easy way and the efficient way.

Part 1 will show a very simple process to call an external weather feed that returns XML data to display in your site.

In Part 2, I will present an efficient way (using iolets and ehcache) to pull back an XML feed and cache the result for hours (reducing the number of out bound URL calls).

The Basic Idea

weather.xml
<dynaments>
  <rde-dm:include tag="weather_google" 
                  content="http://www.google.com/ig/api?weather=Calgary" />
</dynaments>

weather.xsl
<?xml version="1.0" encoding="UTF-8"?>
<templates>
    <xsl:template match="weather">
        <xsl:for-each select="forecast_conditions">
           <xsl:value-of select="condition/@data"/>
        </xsl:for-each>
    </xsl:template>
</templates>

You could add the weather.xsl to your primary xsl file (such as projectname.xml or hs.xsl) - this would then automatically present the weather properly on the screen.


Enhance the Idea

weather.xml
<dynaments>
  <rde-dm:attribute mode="condition" attribute="user:preferences.widget.weather.location" op="ne" value="" tag="notag">
 <rde-dm:if>
  <rde-dm:attribute mode="write" attribute="request:weather_url" value="http://www.google.com/ig/api?weather=" inline-function="concat(user:preferences.widget.weather.location)" tag="notag" />
  <rde-dm:include tag="weather_google" 
      content="request:weather_url" />
 </rde-dm:if>
 <rde-dm:else>
  <rde-dm:include content="NoWeatherWidget.htm" tag="notag" />
 </rde-dm:else>
  </attribute>
</dynaments>

Using user attributes (previously assigned when you log in) you can dynamically call the weather service to pull back customized weather.  The general idea is to create a temporary variable with the entire weather url and append the user attribute user:preferences.widget.weather.location.

1) If user attribute exists:
  Display the weather widget

2) If no user attribute defined
   Include HTML tag that allows users to customize location

No comments:

Post a Comment