Having been here nearly 7 years Im sure it has been as hot as it was this weekend but I guess you forget how hot it was last summer. Still, its great to be here having to gripe about how hot it is and to have friends that have a pool. Back in the UK, we would be complaining about the rain ... maybe not this past week thou and as for a pool ... I guess it could double as an ice rink in the winter.
To keep in with the weather theme, I thought I would cover how to get an external data source into your output, RSS feeds are now pervasive across the web, providing news, weather, travel and jokes, you can even get one for this blog. Of course you may have a web service that can serve up XML that can be incorporated into your reports.
I'm going to deal with an RSS weather feed from Yahoo!, they have a very nice, customizable feed that accepts a zip code for those in the USA and for the est of the world (an the US) they have a location id, more details here.
For Oracle HQ we have the following URL passing the zip code for our area:
For my home town in the UK:
Sunny, 110 F
Mon - Mostly Clear. High: 97 Low: 64
Tue - Mostly Sunny. High: 92 Low: 60
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
This generates standard RSS formatted XML thus:
<title>Yahoo! Weather - Redwood City, CA</title>
<description>Yahoo! Weather for Redwood City, CA</description>
<lastBuildDate>Mon, 24 Jul 2006 12:47 pm PDT</lastBuildDate>
<yweather:location city="Redwood City" region="CA" country="US"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="88" direction="340" speed="12"/>
<yweather:atmosphere humidity="29" visibility="32000" pressure="29.72" rising="2"/>
<yweather:astronomy sunrise="6:07 am" sunset="8:24 pm"/>
We can incorporate this content into our template quite simply. If you have XML Publisher enterprise you have a couple of options:
- Create a URL based data source, this can be combined with other sources by the extraction engine to generate a combined data set. You can then reference the weather data directly in your template.
- For those of you using Oracle Applications or an XMLP API you can use a native XSL function to pull the document in at runtime. Its called simply document(). It takes a URI as an argument and you can easily load the document into a variable thus:
We can then reference our variable which is actually holding the complete RSS document and build whatever layout we wish against it.
So there are a couple of things we need to be wary of when navigating this RSS XML in our template.
- We need to declare the variable before we write any fields against it. If we dont we're going to get an error.
- We then need to create a loop over the document: <?for-each:$YWeather/*?>
- If you take a look at the XML you'll notice many of the elements have a 'yweather:' prefix. This is a namesace that needs to be declared in our template too so that the engine understands the elements at runtime. Its a simple declaration:
<?namespace:yweather=http://xml.weather.yahoo.com/ns/rss/1.0?> - the URL is taken from the XML.
Once the namespace is declared we can reference the elements safely e.g. <?yweather:location/@city?> - you may have noticed the XML makes the use of attributes for the data. We reference those using the "@" character.
- Lastly, the data references an image to give the user an idea of the weather, sunny, partially cloudy (I prefer the term mostly sunny, cos my glass is always half full :o), etc. Now I had to do some funky stuff to reference the image. The RSS XML does not have a clear URL to the image in an element. Its there in the 'description' element embedded inside some HTML but I did not want to parse that out for now, we'll save that for another time. So I'm still using a dummy image and then constructing a URL using the concat command but the XMLP parser gets upset when it sees the namespace, we end up with something like:
which does not get resolved correctly. So I created a variable 'WImg' to hold the image number:
you need the @incontext to get the variable declaration in the correct place. We can then use the concat function to build the image URL:
Once we have all this the rest of the template is straightforward stuff. You can find a sample here
. Dont forget if you are running behind a firewall you need to set your java prefs so that XMLP can get outside and retrieve this stuff.
Now for a quick dip in that pool and something icy to drink ...