XQuery/SPARQL interface

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

The following script provides, via a Joseki server at UWE, a query interface to RDF. Literal language and datatype are ignored in this representation. URIs link to the browse query and also directly to the resource.

A function converts the SPARQL XML Query result to a table, with links.

declare function fr:sparql-to-table($rdfxml,$script-name ) {
(: literal language and datatype ignored in this representation.  URI links to the browse query and directly to the resource are generated :)
    let $vars := $rdfxml//sr:head/sr:variable/@name
  <table border="1">
      {for $var in  $vars
    { for  $row in $rdfxml//sr:results/sr:result
            { for $var in $vars
              let $binding  := $row/sr:binding[@name=$var]/*          
                  {  typeswitch ($binding)
                        case element(sr:uri)  return
                           (<a href="{$script-name}?uri={string($binding)}">{ string($binding) }</a>,  
                            <a href="{string($binding)}"> ^ </a> 
                       case element(sr:literal) return
                       case element (sr:bnode) return
                        default  return

The SPARQL interface uses the configuration file to declare the namespaces.

import module namespace fr="http://www.cems.uwe.ac.uk/wiki/fr"   at  "fr.xqm";
declare namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace rdfs = "http://www.w3.org/2000/01/rdf-schema#";

declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=no indent=yes 
declare variable $config-file := request:get-parameter("config", "/db/Wiki/RDF/empdeptconfig.xml");
declare variable $config := doc($config-file);
declare variable $graph :=  concat("http://www.cems.uwe.ac.uk/xmlwiki/RDF/xml2rdf.xq?config=",$config-file);
declare variable $default-engine := "http://www.cems.uwe.ac.uk/joseki/sparql";
declare variable $script-name :=   tokenize(request:get-uri(),'/')[last()];
declare variable $default-prolog :=
"PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
PREFIX afn: <http://jena.hpl.hp.com/ARQ/functions#>

declare variable $browse := "select ?s ?p ?o 
where {
      {<uri> ?p ?o }
      {?s ?p <uri>}
      {?s <uri> ?o}
let $config-prolog := fr:sparql-prefixes($config)
let $query := request:get-parameter ("query",())
let $uri := request:get-parameter("uri",())   
let $engine := request:get-parameter("engine",$default-engine)
let $query := 
   if ($uri)
   then  replace($browse,"uri",$uri)
   else $query
let $queryx := concat($default-prolog,$config-prolog,$query)
let $sparql := concat($engine,
let $result  :=
       if ($query !="")
          fr:sparql-to-table(doc($sparql), $script-name) 
       else ()
<html xmlns="http://www.w3.org/1999/xhtml">
      <title>Emp-dept Query</title>
      <h1>Emp-dept Query</h1>
      <form action="{$script-name}">
          <textarea name="query" rows ="8" cols="90">
        <input type="submit"/>

Application[edit | edit source]


The interface expands a query like

  select ?name ?job where {
   ?emp rdf:type  f:emp.
   ?emp foaf:surname ?name.
   ?emp f:Job ?job.


 prefix foaf: <http://xmlns.com/foaf/0.1/>
 prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 prefix f: <http://www.cems.uwe.ac.uk/xmlwiki/empdept/concept/>
 prefix xs: <http://www.w3.org/2001/XMLSchema#>
 select ?name ?job 
 from <http://www.cems.uwe.ac.uk/xmlwiki/RDF/xml2rdf.xq?config=/db/Wiki/RDF/empdeptconfig.xml">
 where {
   ?emp rdf:type  f:emp.
   ?emp foaf:surname ?name.
   ?emp f:Job ?job.

and sends this to the Joseki service. The graph to query is actually passed as the default graph rather than in the from clause.

To do[edit | edit source]

  • handle language and datatype
  • local URIs as local names rather than full URIs#
  • better handling of default graph - should be able to reference the cached rdf defined in the config file