XQuery/SPARQL 介面
外觀
< XQuery
以下指令碼透過 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 XHTML 1.0 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,
"&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