• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

XLST limit result set

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 as to only return three <  <div class="forecast_conditions"> divs - eg.;

<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>

Open in new window

0
sonic1234
Asked:
sonic1234
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
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
 
Geert BormansCommented:
sorry, this is a paste in the wrong question,
well the posted XSLT supports your other question,
add the below template for limiting the records
<xsl:template match="weather">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(),'_data')"/>
            </xsl:attribute>
            <xsl:apply-templates select="forecast_information|current_conditions|forecast_conditions[count(preceding-sibling::forecast_conditions) &lt; 3]|forecast_date|@data"/>
        </div>
     </xsl:template>

Open in new window

0
 
sonic1234Author Commented:
Sorry Gertone -  I tried the above but it mangled my output due to some error I am making - no doubt I am adding code to the wrong location - could you post the complete XSLT please?
0
 
Geert BormansCommented:

<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="weather">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(),'_data')"/>
            </xsl:attribute>
            <xsl:apply-templates select="forecast_information|current_conditions|forecast_conditions[count(preceding-sibling::forecast_conditions) &lt; 3]|forecast_date|@data"/>
        </div>
     </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
 
sonic1234Author Commented:
Thank you very much Gertone - works perfectly
0

Featured Post

Independent Software Vendors: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now