XQuery/DBpedia with SPARQL and Simile Timeline - Album Chronology

In this example, the SIMILE project's Timeline JavaScript is used to display the chronology of the albums released by a selected artist or group. As with the football teams in the previous example, the data is extracted from DBpedia via a SPARQL query.

This example has three components:
 * an XQuery script which creates the HTML page, linking to the Timeline JavaScript
 * an XQuery script to query DBpedia for albums for a selected group, assign a year of release to each album and generate a set of events in the form expected by the TimeLine script.
 * an XQuery script to provide an index of groups in a given category

HTML Page script
The script accepts one parameter, the name of the artist or group. The JavaScript required to customize the Timeline is defined in-line in a CDATA section. The onLoad function accepts two parameters, the name of the artist and a date to provide the initial focus for the timeline. The event stream is provided by the call to the XQuery script group2tl.xq, passing the group name.

The JavaScript interface here is minimal.

declare option exist:serialize "method=xhtml media-type=text/html";

let $group:= request:get-parameter("group","Eagles") return   <![CDATA[ function onLoad(group,start) { var theme = Timeline.ClassicTheme.create; theme.event.label.width = 400; // px theme.event.bubble.width = 300; theme.event.bubble.height = 300;

var eventSource1 = new Timeline.DefaultEventSource;

var bandInfo = [ Timeline.createBandInfo({       eventSource:    eventSource1,        theme:          theme,        date:           start,        width:          "100%",         intervalUnit:   Timeline.DateTime.YEAR,         intervalPixels: 45    }),

]; Timeline.create(document.getElementById("my-timeline"), bandInfo); Timeline.loadXML("group2tl.xq?group="+group, function(xml, url) { eventSource1.loadXML(xml, url); });

} ]]>    {$group} Albums

TimeLine events group2tl.xq
This script has a similar structure to the football team script. A prototype SPARQL query is edited to change the default name to the supplied name, the query sent to the DBpedia SPARQL service and the resultant SPARQL XML result converted to XML tuples.

Note - the group name needs an additional encoding because SPARQL requires URIs to be uri encoded and group names may contain characters such as ( and ) which require encoding. Hence Queen_(band) must appear in the URI in SPARQL as Queen_%28band%29. In addition the whole query is uri encoded which allows this encoding to be recovered when the whole query is decoded by the SPARQL service. (Whew!)

As with the Football clubs, there may be multiple tuples for the same album and the best (at present just the first non null) set of data needs to be extracted from these tuples. The year of release is hacked out of the multitude of different date formats which may appear and each album represented in the format expected by Timeline, with the pop-up contents serialised. This is limited to the album cover if available and links to DBpedia and Wikipedia.

declare namespace r = "http://www.w3.org/2005/sparql-results#";

declare variable $query := " PREFIX p:  PREFIX rdf:  SELECT * WHERE {      ?album p:artist  .            ?album rdf:type .     OPTIONAL {?album p:cover ?cover}.     OPTIONAL {?album p:name ?name}.     OPTIONAL {?album p:released ?dateofrelease}.   } ";

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=",                   encode-for-uri($query)                  ) return doc($sparql) };

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

declare function local:clean($text) { let $text:= util:unescape-uri($text,"UTF-8") let $text := replace($text,"http://dbpedia.org/resource/","") let $text := replace($text,"\(.*\)","") let $text := replace($text,"_"," ") return $text };

declare function local:year-from-date($d) { let $d := replace($d,"[^0-9\-]","") let $dp := tokenize($d,"-") let $year := $dp[1] return if ($year castable as xs:integer and string-length($year)=4) then $year else }; let $group := request:get-parameter ("group","The_Allman_Brothers_Band") let $groupx := replace($group," ","_") let $queryx := replace($query,"The_Allman_Brothers_Band",encode-for-uri($group)) let $result := local:execute-sparql($queryx) let $tuples := local:sparql-to-tuples($result) return

Execution

 * Pink Floyd
 * Leonard Cohen

Group Index
This script queries DBpedia for the resources which belong to a specified category, for example Rock_and_Roll_Hall_of_Fame_inductees. A table of group names in alphabetical order provides links to the Timeline view using the script above, and to an HTML table view of the discography.

declare namespace r = "http://www.w3.org/2005/sparql-results#";

declare option exist:serialize "method=xhtml media-type=text/html"; declare variable $query := " PREFIX skos:  PREFIX p:  SELECT * WHERE {      ?group  skos:subject .   } "; 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=",                  escape-uri($query,true)                   ) return doc($sparql) };

declare function local:clean($text) { let $text:= util:unescape-uri($text,"UTF-8") let $text := replace($text,"\(.*\)","") let $text := replace($text,"_"," ") return $text };

let $category := request:get-parameter("category","Rock_and_Roll_Hall_of_Fame_inductees") let $queryx := replace($query,"Rock_and_Roll_Hall_of_Fame_inductees",$category) let $result := local:execute-sparql($queryx) return {local:clean($category)}

Index of Rock and Roll Groups and Artists