XQuery/SPARQL interface

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
    return
  <table border="1">
    <tr>
      {for $var in  $vars
         return
          <th>{string($var)}</th>
      }
    </tr>
    { for  $row in $rdfxml//sr:results/sr:result
     return
     <tr>
            { for $var in $vars
              let $binding  := $row/sr:binding[@name=$var]/*          
              return                
                 <td>
                  {  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
                          string($binding)    
                       case element (sr:bnode) return
                           concat("_:",$binding)
                        default  return
                            ()
                    }
                 </td>
             }
      </tr>
      }
   </table>
 };

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 
        doctype-public=-//W3C//DTD&#160;XHTML&#160;1.0&#160;Transitional//EN
        doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
 
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 }
      UNION
      {?s ?p <uri>}
      UNION
      {?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,
                      "?default-graph-uri=",$graph,
                      "&amp;query=",encode-for-uri($queryx) 
                     )
let $result  :=
       if ($query !="")
       then
          fr:sparql-to-table(doc($sparql), $script-name) 
       else ()
return
<html xmlns="http://www.w3.org/1999/xhtml">
   <head> 
      <title>Emp-dept Query</title>
   </head>
   <body>
      <h1>Emp-dept Query</h1>
      <form action="{$script-name}">
          <textarea name="query" rows ="8" cols="90">
             {$query}
        </textarea>
        <br/>
        <input type="submit"/>
     </form>
     <h2>Result</h2>
         {$result}
    </body>
</html>

ApplicationEdit

Query


The interface expands a query like

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

into:

 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 doEdit

  • 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
Last modified on 5 March 2008, at 08:01