跳转到内容

XQuery/SPARQL 接口

来自维基教科书,开放的书籍,开放的世界

以下脚本通过 UWE 上的 Joseki 服务器提供对 RDF 的查询接口。此表示形式中忽略了文字语言和数据类型。URI 链接到浏览查询以及直接链接到资源。

一个函数将 SPARQL XML 查询结果转换为表格,带有链接。

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>
 };

SPARQL 接口使用配置文件来声明命名空间。

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>

查询


该接口扩展了类似于以下的查询

  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.
 }

并将此发送到 Joseki 服务。要查询的图形实际上作为默认图形传递,而不是在 from 子句中。

待办事项

[编辑 | 编辑源代码]
  • 处理语言和数据类型
  • 本地 URI 作为本地名称,而不是完整 URI#
  • 更好地处理默认图形 - 应该能够引用配置文件中定义的缓存 rdf
华夏公益教科书