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

x
?
Solved

XSLT Mofification for Weather Feed

Posted on 2011-10-25
3
Medium Priority
?
218 Views
Last Modified: 2012-05-12
I'm calling the following XML feed;

http://www.google.co.uk/ig/api?weather=tamworth%20nsw

Being transformed with the following XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="html"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/*">
        <xsl:apply-templates select="node()"/>
    </xsl:template>

    <xsl:template match="*">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(),'_data')"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()|@data"/>
        </div>
    </xsl:template>
    
    <xsl:template match="forecast_information|current_conditions|forecast_conditions|forecast_date">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="name()"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()|@data"/>
        </div>
    </xsl:template>
 
     <xsl:template match="icon">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="name()"/>
            </xsl:attribute>
           <img src="/cp_themes/default{@data}"/>
        </div>
    </xsl:template>
       
    <xsl:template match="postal_code|latitude_e6|longitude_e6|temp_f"/>

    <xsl:template match="low | high">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(), '_data')"/>
            </xsl:attribute>
            <xsl:call-template name="farenheit-to-celsius">
                <xsl:with-param name="temp" select="@data"/>
            </xsl:call-template>
        </div>
    </xsl:template>
    
    <xsl:template match="*" mode="id">
        <xsl:value-of select="concat(name(),'_data')"/>
    </xsl:template>
    
    <xsl:template name="farenheit-to-celsius">
        <xsl:param name="temp"/>
        <xsl:choose>
            <xsl:when test="not(string(number($temp)) = 'NAN')">
                <xsl:value-of select="format-number(($temp - 32) * (5 div 9) , '##.0')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$temp"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Open in new window


Creates the following HTML

<div class="weather_data">
  <div class="forecast_information">
    <div class="city_data">tamworth, NSW</div>
    <div class="forecast_date">2011-10-26</div>
    <div class="current_date_time_data">2011-10-26 14:00:00 +0000</div>
    <div class="unit_system_data">US</div>
  </div>
  <div class="current_conditions">
    <div class="condition_data"></div>
    <div class="temp_c_data">17</div>
    <div class="humidity_data">Humidity: 46%</div>
    <div class="icon"><img src="/cp_themes/default"></div>
    <div class="wind_condition_data">Wind: E at 18 mph</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Wed</div>
    <div class="low_data">12.2</div>
    <div class="high_data">23.9</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/mostly_sunny.gif"></div>
    <div class="condition_data">Partly Sunny</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Thu</div>
    <div class="low_data">13.9</div>
    <div class="high_data">26.1</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/sunny.gif"></div>
    <div class="condition_data">Clear</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Fri</div>
    <div class="low_data">17.8</div>
    <div class="high_data">30.0</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/cloudy.gif"></div>
    <div class="condition_data">Cloudy</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Sat</div>
    <div class="low_data">16.1</div>
    <div class="high_data">28.9</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/chance_of_storm.gif"></div>
    <div class="condition_data">Chance of Storm</div>
  </div>
</div>

Open in new window


How can I modify the XSLT so that the value for the first <div class="forecast_conditions"> <div class="icon"> is written to the <div class="current_conditions">  <div class="icon">

Thus the output would be something like this  - note the change on line 12, which is getting the same value as line 19;;

<div class="weather_data">
  <div class="forecast_information">
    <div class="city_data">tamworth, NSW</div>
    <div class="forecast_date">2011-10-26</div>
    <div class="current_date_time_data">2011-10-26 14:00:00 +0000</div>
    <div class="unit_system_data">US</div>
  </div>
  <div class="current_conditions">
    <div class="condition_data"></div>
    <div class="temp_c_data">17</div>
    <div class="humidity_data">Humidity: 46%</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/mostly_sunny.gif"></div>
    <div class="wind_condition_data">Wind: E at 18 mph</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Wed</div>
    <div class="low_data">12.2</div>
    <div class="high_data">23.9</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/mostly_sunny.gif"></div>
    <div class="condition_data">Partly Sunny</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Thu</div>
    <div class="low_data">13.9</div>
    <div class="high_data">26.1</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/sunny.gif"></div>
    <div class="condition_data">Clear</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Fri</div>
    <div class="low_data">17.8</div>
    <div class="high_data">30.0</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/cloudy.gif"></div>
    <div class="condition_data">Cloudy</div>
  </div>
  <div class="forecast_conditions">
    <div class="day_of_week_data">Sat</div>
    <div class="low_data">16.1</div>
    <div class="high_data">28.9</div>
    <div class="icon"><img src="/cp_themes/default/ig/images/weather/chance_of_storm.gif"></div>
    <div class="condition_data">Chance of Storm</div>
  </div>
</div>

Open in new window

0
Comment
Question by:sonic1234
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 37028664
Try this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output indent="yes" method="html"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/*">
        <xsl:apply-templates select="node()"/>
    </xsl:template>
    
    <xsl:template match="*">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(),'_data')"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()|@data"/>
        </div>
    </xsl:template>
    
    <xsl:template match="forecast_information|current_conditions|forecast_conditions|forecast_date">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="name()"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()|@data"/>
        </div>
    </xsl:template>
    
    <xsl:template match="current_conditions/icon">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="name()"/>
            </xsl:attribute>
            <img src="/cp_themes/default{ancestor::weather/forecast_conditions[1]/icon/@data}"/>
        </div>
    </xsl:template>
    
    <xsl:template match="forecast_conditions/icon">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="name()"/>
            </xsl:attribute>
            <img src="/cp_themes/default{@data}"/>
        </div>
    </xsl:template>
    
    <xsl:template match="postal_code|latitude_e6|longitude_e6|temp_f"/>
    
    <xsl:template match="low | high">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(), '_data')"/>
            </xsl:attribute>
            <xsl:call-template name="farenheit-to-celsius">
                <xsl:with-param name="temp" select="@data"/>
            </xsl:call-template>
        </div>
    </xsl:template>
    
    <xsl:template match="*" mode="id">
        <xsl:value-of select="concat(name(),'_data')"/>
    </xsl:template>
    
    <xsl:template name="farenheit-to-celsius">
        <xsl:param name="temp"/>
        <xsl:choose>
            <xsl:when test="not(string(number($temp)) = 'NAN')">
                <xsl:value-of select="format-number(($temp - 32) * (5 div 9) , '##.0')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$temp"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Closing Comment

by:sonic1234
ID: 37028707
Perfect as usual. thank you.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37028714
welcome
0

Featured Post

Industry Leaders: 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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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 video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

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