# XQuery/DBpedia with SPARQL - Football teams

DBpedia is a project to convert the contents of Wikipedia to RDF so that it can be linked into other datasets to add to the Semantic Web. A w:SPARQL endpoint is provided to query this database.

## SPARQL to KML

This application uses DBpedia to create a kml file showing the birth places of the members of a selected UK Football team. Data quality is limited by a number of factors:
* the age of the Wikipedia extract on which DBpedia is based
* the existence or non-existence of individual pages in Wikipedia for players
* the consistency of property labeling on Wikipedia infoboxes

### SPARQL Query

This query is complicated by the need to handle possible redirection of the city name - (can this be improved - this is a generic problem?). To obtain more complete data, the query should also handle the multiple synonyms used for place and date of birth
Changes to dbpedia lead to a short life for queries based om the data-model and vocabulary. As of Jan 2011, the query is being updated. Currently to get locations and birthdates for the current players at Arsenal, the following query seems to work.
However this yields multiple geocoded locations, of which it can be assumed that the first is most specific (but not possible ? to filter in SPARQL).

### DBpedia Query

The prototype SPARQL query is targeted on Arsenal_F.C. This team name needs to be replaced by the supplied team name, the query then URI-encoded and passed to the DBpedia SPARQL endpoint.
Aside: Initially, the query was written with a generic placeholder ($team) rather than a protypical value (Arsenal_F.C.). The prototype idiom has the benefit of providing an executable SPARQL query without editing, is more expressive and less tricky - the $ in $team needs escaping in the replace expression since the second argument is a regular expression.

### Execute the SPARQL Query

This query uses the SPARQL endpoint provided by the Virtuoso engine. The format of the result is defined to be XML i.e. the SPARQL Query Result format. A function tidies up the interface:

### DBpedia Result

The result is in SPARQL Query Results XML format. It is more convenient to convert this generic format to tuples with named elements for later processing.

### KML output

Since we are generating kml, we need to set the media type and file name and create a Document node - in the appropriate places in the script:
The icon is a stock GoogleEarth footballer icon.

### Document construction

Due to the multiple values for some of the properties, for example cityofbirth is often expressed as an address path, there are multiple tuples for each player. These need grouping and compressing. Here we use the XQuery idiom which uses distinct-values to get a set of player names, and then accesses groups of rows with the name as the key. This scripts takes a simplistic approach of using only the first of multiple tuples which contains a latitude , pending a better resolution of the multiple cityofbirth values.
We are only interested in players whose place of birth has been geo-coded, so we filter for tuples with a latitude element:

### Data cleanup

The wikiPedia data needs some clean-up before being usable in the kml. A generic clean function decodes the uri-encoded characters, removes some irrelevant text and replaces underscores with spaces. ( this hack needs improving )

### Data typing

The date of birth is in the form xs:date, but is optional. If the value is a valid date, it is converted to a more readable form using an eXist function:
Similarly for the position number which should be an xs:integer:
The latitude and longitude should be xs:decimal. Since sometimes several players in a team come from the same place, the mapped positions are dithered a little.

### Placemark Construction

The body of the Placemark description will contain XHTML markup to display an image if there is one and to link to the DBpedia page. The XML needs to be serialised to a string for GoogleMap to render the description in a pop-up:

### Execute

Map of Arsenal players:
* generate the kml
* link to GoogleMaps
Note that the q parameter is URI-encoded.

### Complete Script
## Club Index

We also need an index page, selecting all Clubs in the major English and Scottish leagues. This script follows the same lines as the more complex script above, except that due to the simpler data, the raw SPARQL result is used without transformation.
The index is sorted alphabetically by club name and provides links to the player map and to the base DBpedia data.

### Club Index

Club Index