Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2005, 11:11 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default <xsl:if test="" Question

I'm going to be using a few RSS/XML feeds for our new site, including the National Weather Services "Current Conditions" feed at http://www.nws.noaa.gov/data/current_obs/KLWC.xml. I'm also going to use their built-in icons with this feed, which can be found at http://www.nws.noaa.gov/data/current_obs/weather.php.

Everything's going very well so far, but I have a question concerning the xsl:if method. Is there an easier way to use multiple filters in the test function of this method? Right now, I'm using this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:variable name="var_weather" select="document('http://www.nws.noaa.gov/data/current_obs/KLWC.xml')" /> <xsl:param name="par_currentweather" select="$var_weather/current_observation/weather" /> <xsl:template match="/">  <xsl:if test="$par_currentweather = 'Mostly Cloudy' or $par_currentweather = 'Mostly Cloudy with Haze'"> <a href="http://www.crh.noaa.gov/ifps/MapClick.php?CityName=Lawrence&state=KS&site=TOP" target="_blank"> <img title="{$par_currentweather}" height="58" alt="{$par_currentweather}" src="http://weather.gov/weather/images/fcicons/bkn.jpg" width="55" /> </a> </xsl:if>  <xsl:if test="$par_currentweather = 'Fair' or $par_currentweather = 'Clear'"> <a href="http://www.crh.noaa.gov/ifps/MapClick.php?CityName=Lawrence&state=KS&site=TOP" target="_blank"> <img title="{$par_currentweather}" height="58" alt="{$par_currentweather}" src="http://weather.gov/weather/images/fcicons/skc.jpg" width="55" /> </a> </xsl:if> </xsl:template> </xsl:stylesheet>
...but because some of the "current weather" filters are as long as this one:

Thunderstorm | Light Thunderstorm Rain | Heavy Thunderstorm Rain | Thunderstorm Rain Fog/Mist | Light Thunderstorm Rain Fog/Mist | Heavy Thunderstorm Rain Fog/Mist | Thunderstorm Showers in Vicinity | | Light Thunderstorm Rain Haze | Heavy Thunderstorm Rain Haze | Thunderstorm Fog | Light Thunderstorm Rain Fog | Heavy Thunderstorm Rain Fog | Thunderstorm Light Rain | Thunderstorm Heavy Rain | Thunderstorm Rain Fog/Mist | Thunderstorm Light Rain Fog/Mist | Thunderstorm Heavy Rain Fog/Mist | Thunderstorm in Vicinity Fog/Mist | Thunderstorm Showers in Vicinity | Thunderstorm in Vicinity | Thunderstorm in Vicinity Haze | Thunderstorm Haze in Vicinity | Thunderstorm Light Rain Haze | Thunderstorm Heavy Rain Haze | Thunderstorm Fog | Thunderstorm Light Rain Fog | Thunderstorm Heavy Rain Fog | Thunderstorm Hail | Light Thunderstorm Rain Hail | Heavy Thunderstorm Rain Hail | Thunderstorm Rain Hail Fog/Mist | Light Thunderstorm Rain Hail Fog/Mist | Heavy Thunderstorm Rain Hail Fog/Mist | Thunderstorm Showers in Vicinity Hail | | Light Thunderstorm Rain Hail Haze | Heavy Thunderstorm Rain Hail Haze | Thunderstorm Hail Fog | Light Thunderstorm Rain Hail Fog | Heavy Thunderstorm Rain Hail Fog | Thunderstorm Light Rain Hail | Thunderstorm Heavy Rain Hail | Thunderstorm Rain Hail Fog/Mist | Thunderstorm Light Rain Hail Fog/Mist | Thunderstorm Heavy Rain Hail Fog/Mist | Thunderstorm in Vicinity Hail Fog/Mist | Thunderstorm Showers in Vicinity Hail | Thunderstorm in Vicinity Hail | Thunderstorm in Vicinity Hail Haze | Thunderstorm Haze in Vicinity Hail | Thunderstorm Light Rain Hail Haze | Thunderstorm Heavy Rain Hail Haze | Thunderstorm Hail Fog | Thunderstorm Light Rain Hail Fog | Thunderstorm Heavy Rain Hail Fog | Thunderstorm Small Hail/Snow Pellets | Thunderstorm Rain Small Hail/Snow Pellets | Light Thunderstorm Rain Small Hail/Snow Pellets | Heavy Thunderstorm Rain Small Hail/Snow Pellets

...I'd obviously like to find a simpler way of doing this. If anyone knows of such a way, I'd LOVE to know it. Thanks for any help.

KWilliams
 
Old August 8th, 2005, 05:21 PM
Registered User
 
Join Date: Aug 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I where you, I'd forget about the if statements. Don't use them. Ever. Okay, maybe sometimes, but not now. Use templates instead. Why don't you create an template for each weather type, and match on it? It might not make your XSLT smaller, but it could help in making it a lot easier to maintain. Eg, a new type of weather (and icon) would only involve creating a new template. I'm not sure if that's what you want, if not, please let me know!

 
Old August 9th, 2005, 08:30 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Bodiam,

That's a great sugguestion, and I'll give it a try, but that doesn't solve my initial problem concerning the test methods...whether you're using xsl:if or xsl:template.

For instance, if you had this set of filters for one icon:
Thunderstorm | Light Thunderstorm Rain | Heavy Thunderstorm Rain | Thunderstorm Rain Fog/Mist | Light Thunderstorm Rain Fog/Mist | Heavy Thunderstorm Rain Fog/Mist | Thunderstorm Showers in Vicinity | | Light Thunderstorm Rain Haze | Heavy Thunderstorm Rain Haze | Thunderstorm Fog | Light Thunderstorm Rain Fog | Heavy Thunderstorm Rain Fog | Thunderstorm Light Rain | Thunderstorm Heavy Rain | Thunderstorm Rain Fog/Mist | Thunderstorm Light Rain Fog/Mist | Thunderstorm Heavy Rain Fog/Mist | Thunderstorm in Vicinity Fog/Mist | Thunderstorm Showers in Vicinity | Thunderstorm in Vicinity | Thunderstorm in Vicinity Haze | Thunderstorm Haze in Vicinity | Thunderstorm Light Rain Haze | Thunderstorm Heavy Rain Haze | Thunderstorm Fog | Thunderstorm Light Rain Fog | Thunderstorm Heavy Rain Fog | Thunderstorm Hail | Light Thunderstorm Rain Hail | Heavy Thunderstorm Rain Hail | Thunderstorm Rain Hail Fog/Mist | Light Thunderstorm Rain Hail Fog/Mist | Heavy Thunderstorm Rain Hail Fog/Mist | Thunderstorm Showers in Vicinity Hail | | Light Thunderstorm Rain Hail Haze | Heavy Thunderstorm Rain Hail Haze | Thunderstorm Hail Fog | Light Thunderstorm Rain Hail Fog | Heavy Thunderstorm Rain Hail Fog | Thunderstorm Light Rain Hail | Thunderstorm Heavy Rain Hail | Thunderstorm Rain Hail Fog/Mist | Thunderstorm Light Rain Hail Fog/Mist | Thunderstorm Heavy Rain Hail Fog/Mist | Thunderstorm in Vicinity Hail Fog/Mist | Thunderstorm Showers in Vicinity Hail | Thunderstorm in Vicinity Hail | Thunderstorm in Vicinity Hail Haze | Thunderstorm Haze in Vicinity Hail | Thunderstorm Light Rain Hail Haze | Thunderstorm Heavy Rain Hail Haze | Thunderstorm Hail Fog | Thunderstorm Light Rain Hail Fog | Thunderstorm Heavy Rain Hail Fog | Thunderstorm Small Hail/Snow Pellets | Thunderstorm Rain Small Hail/Snow Pellets | Light Thunderstorm Rain Small Hail/Snow Pellets | Heavy Thunderstorm Rain Small Hail/Snow Pellets

I normally would set it up like this:

<xsl:if test="$par_weather = 'Thunderstorm' or $par_weather = 'Light Thunderstorm Rain' or $par_weather = 'Heavy Thunderstorm Rain' or $par_weather = 'Thunderstorm Rain Fog/Mist'

 or $par_weather = 'Thunderstorm Showers in Vicinity' or $par_weather = 'Light Thunderstorm Rain Haze' or $par_weather = 'Heavy Thunderstorm Rain Haze' or $par_weather = 'Thunderstorm Fog' or $par_weather = 'Light Thunderstorm Rain Fog' or $par_weather = 'Heavy Thunderstorm Rain Fog' or $par_weather = 'Thunderstorm Light Rain' or $par_weather = 'Thunderstorm Heavy Rain' or $par_weather = 'Thunderstorm Rain Fog/Mist' or $par_weather = 'Thunderstorm Light Rain Fog/Mist' or $par_weather = 'Thunderstorm Heavy Rain Fog/Mist' or $par_weather = 'Thunderstorm in Vicinity Fog/Mist' or $par_weather = 'Thunderstorm Showers in Vicinity' or $par_weather = 'Thunderstorm in Vicinity' or $par_weather = 'Thunderstorm in Vicinity Haze' or $par_weather = 'Thunderstorm Haze in Vicinity' or $par_weather = 'Thunderstorm Light Rain Haze' or $par_weather = 'Thunderstorm Heavy Rain Haze' or $par_weather = 'Thunderstorm Fog' or $par_weather = 'Thunderstorm Light Rain Fog' or $par_weather = 'Thunderstorm Heavy Rain Fog' or $par_weather = 'Thunderstorm Hail' or $par_weather = 'Light Thunderstorm Rain Hail' or $par_weather = 'Heavy Thunderstorm Rain Hail' or $par_weather = 'Thunderstorm Rain Hail Fog/Mist' or $par_weather = 'Light Thunderstorm Rain Hail Fog/Mist' or $par_weather = 'Heavy Thunderstorm Rain Hail Fog/Mist' or $par_weather = 'Thunderstorm Showers in Vicinity Hail' or $par_weather = 'Light Thunderstorm Rain Hail Haze' or $par_weather = 'Heavy Thunderstorm Rain Hail Haze' or $par_weather = 'Thunderstorm Hail Fog' or $par_weather = 'Light Thunderstorm Rain Hail Fog' or $par_weather = 'Heavy Thunderstorm Rain Hail Fog' or $par_weather = 'Thunderstorm Light Rain Hail' or $par_weather = 'Thunderstorm Heavy Rain Hail' or $par_weather = 'Thunderstorm Rain Hail Fog/Mist' or $par_weather = 'Thunderstorm Light Rain Hail Fog/Mist' or $par_weather = 'Thunderstorm Heavy Rain Hail Fog/Mist' or $par_weather = 'Thunderstorm in Vicinity Hail Fog/Mist' or $par_weather = 'Thunderstorm Showers in Vicinity Hail' or $par_weather = 'Thunderstorm in Vicinity Hail' or $par_weather = 'Thunderstorm in Vicinity Hail Haze' or $par_weather = 'Thunderstorm Haze in Vicinity Hail' or $par_weather = 'Thunderstorm Light Rain Hail Haze' or $par_weather = 'Thunderstorm Heavy Rain Hail Haze' or $par_weather = 'Thunderstorm Hail Fog' or $par_weather = 'Thunderstorm Light Rain Hail Fog' or $par_weather = 'Thunderstorm Heavy Rain Hail Fog' or $par_weather = 'Thunderstorm Small Hail/Snow Pellets' or $par_weather = 'Thunderstorm Rain Small Hail/Snow Pellets' or $par_weather = 'Light Thunderstorm Rain Small Hail/Snow Pellets' or $par_weather = 'Heavy Thunderstorm Rain Small Hail/Snow Pellets'">
<img src="icon.jpg"... />
</xsl:if>

...and this would only be for icon when there are 20 more! I just need to know whether or not there is an alternative way of accomplishing the same thing within the test. Thanks.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 08:06 AM
Usage of '*' in test condition of <xsl:when> vikkiefd XSLT 5 March 12th, 2008 10:31 PM
XSL Test tclancy XSLT 2 February 3rd, 2006 02:49 PM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.