# XQuery/Page scraping and Yahoo Weather
## Background

Yahoo provide a world weather forecast service via a REST API, delivering RSS. It is described in the API documentation.
However the key to each feed for UK towns is a Yahoo Location ID such as UKXX0953 and there is no service available to convert from location names to Yahoo codes. Yahoo does provide alphabetical index pages of locations which contain links to the feeds themselves.

## Yahoo Pipe

This task can be accomplished by the Yahoo Pipe written by Paul Daniel. (up to the extraction of the location ID) However the inherent instability of HTML markup leads to the current failure of this pipeline.

## XQuery

This script takes a location parameter, extracts the first letter of the location, constructs the URL of the yahoo weather index page for that letter, the index page for the letter B and fetches the page via the httpclient module in eXist. The page is not valid XHTML but the httpclient:get function cleans up the XML so it is well-formed.
HTML page
The page structure can be seen in the tree view.
Next this XML is navigated to locate the li element containing the location and strips out the code for that location. Finally this code is appended to the stem of the URL of the RSS page for this location, created a URL for the RSS feed at that location. RSS feed and the script then redirects to that URL.
This process can be visualized using a data flow diagram Diagram
* Bristol RSS feed
* Cardiff RSS feed

### Notes

* Although the index page is not valid XHTML (why not?) and needs tidying, Yahoo have been helpful to the scrapper by using ids on the sections. This allows the XPath expression to pick out the relevant section by id, and then select the li containing the location. However such tagging is not stable, and in fact changed recently from an id of browse to the current yw-regionalloc. Note also that there is additional work required because the page for A has a different URL to the remainder of the letters -a feature not easily seen or tested for.
* eXist is not ideally suited to this task since the page has to be first stored in the database so that XPath expressions can be executed using the structural index. An in-memory XQuery engine such as Saxon would be expected to perform better on this task. At present the performance is a bit slow but the new 1.3 release improves this situation.
* Extracting the code from the string would be clearer with a regular expression, but XQuery does not provide a simple matching function to extract the matched pattern. An XQuery function which wraps some XSLT to do this is described in analyse-string
* The script uses the eXist function response:redirect-to to re-direct the browser to the constructed URL for the RSS feed

## XSLT

For comparison, here is the equivalent XSLT script, using analyse-string.
Bristol Weather - but currently broken

## XPL

Another approach is to use XPL developed by Erik Bruchez and Alessandro Vernet at Orbeon to describe the sequence of transformations as a pipeline. Here the pipeline is extended to create a custom HTML page from the RSS feed.
Given implementations for each of the named processor types, this can be executed (albeit rather slowly in this prototype XQuery processor )
* This is a work in progress - at present this XPL engine is only a very simple, partial prototype, and even this simple sequential example is not conformant with the XPL schema (hence the local namespace).
The pipeline can be visualized using GraphViz.
* The intention is to generate an additional image map to support linking to the underlying processes as well as support the full XPL language