XQuery/DBpedia with SPARQL - Stadium locations

From Wikibooks, open books for an open world
Jump to navigation Jump to search

At the risk of being repetitious, here is another script which mashes up data from DBpedia with GoogleMaps, this time to show the location of all venues in a supplied Wikipedia Category of venues.

Examples[edit | edit source]

Football Venues in England[edit | edit source]

Football Venues in Scotland[edit | edit source]

Script[edit | edit source]

   This accepts a category of stadiums and generates a kml map of all stadiums

declare namespace r = "http://www.w3.org/2005/sparql-results#";
declare variable $query := "
PREFIX p: <http://dbpedia.org/property/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  {?ground skos:subject <http://dbpedia.org/resource/Category:Football_venues_in_England>.
  ?ground geo:long ?long.
  ?ground geo:lat ?lat.
  ?ground rdfs:label ?groundname.
  OPTIONAL {?ground foaf:depiction ?image .}.
  OPTIONAL {?club p:ground ?ground.  ?club rdfs:label ?clubname . FILTER (lang(?clubname) = 'en')}.
  OPTIONAL {?ground foaf:page ?wiki.}.
  FILTER (lang(?groundname) ='en'). 

declare function local:execute-sparql($query as xs:string) {
 let $sparql := concat("http://dbpedia.org/sparql?format=xml&amp;default-graph-uri=http://dbpedia.org&amp;query=",
 return doc($sparql)

declare function local:sparql-to-tuples($rdfxml) {
  for $result in $rdfxml//r:result
      { for $binding in $result/r:binding
         if ($binding/r:uri)
           then  element {$binding/@name} {
                  attribute type {"uri"} , 
           else  element {$binding/@name} {
                  attribute type {$binding/@datatype}, 

declare option exist:serialize "method=xhtml media-type=application/vnd.google-earth.kml+xml highlight-matches=none"; 

let $category := request:get-parameter("category","Football_venues_in_England")
let $queryx := replace($query,"Football_venues_in_England",$category)
let $result:= local:execute-sparql($queryx)
let $tuples := local:sparql-to-tuples($result)

let $x := response:set-header('Content-disposition','Content-disposition: inline;filename=stadiums.kml;')


  <name>{replace($category,"_"," ")}</name> 
  <Style id="stadium">
   for $groundid in distinct-values($tuples/ground)
   let $groundTuples := $tuples[ground=$groundid]
   let $ground := $groundTuples [1]
   let $name := string($ground/groundname)
   let $lat := xs:decimal($ground/lat)
   let $long := xs:decimal($ground/long)
   let $clubs := string-join($groundTuples/clubname,", ")
   let $wiki := string($ground/wiki)
   let $description := 
     Ground of {$clubs} 
     {if ($ground/image) then (<br/>,<img src="{$ground/image}"/>) else () }
      <a href='{$groundid}'>DBpedia</a>
      <a href='{$wiki}'>Wikipedia</a>   
     <a href="http://images.google.co.uk/images?q=stadium+{$name}">Google Images</a>
    <coordinates>{concat($long, ",",$lat,",0")}</coordinates>