p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   <xsl:if test="" Question (http://p2p.wrox.com/showthread.php?t=32281)

kwilliams August 8th, 2005 11:11 AM

<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

Bodiam August 8th, 2005 05:21 PM

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!


kwilliams August 9th, 2005 08:30 AM

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


All times are GMT -4. The time now is 10:07 AM.

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