function querypath_examples_show_sparql_lookup in QueryPath 6
Same name and namespace in other branches
- 7.3 querypath_examples.module \querypath_examples_show_sparql_lookup()
- 7.2 querypath_examples.module \querypath_examples_show_sparql_lookup()
1 call to querypath_examples_show_sparql_lookup()
- querypath_examples_show_sparql_form_validate in ./querypath_examples.module
File
- ./querypath_examples.module, line 337
- The main file for querypath_examples.
Code
function querypath_examples_show_sparql_lookup($term) {
$base_url = 'http://dbpedia.org/sparql';
$query = '
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?uri ?name ?label
WHERE {
?uri foaf:name ?name .
?uri rdfs:label ?label
FILTER (?name = "%s")
FILTER (lang(?label) = "en")
}
';
$sparql = sprintf($query, $term);
$params = array(
'query' => $sparql,
'format' => 'application/sparql-results+xml',
);
$data = http_build_query($params);
$url = $base_url . '?' . $data;
$qp = qp($url, 'head');
$headers = array();
foreach ($qp
->children('variable') as $col) {
$headers[] = $col
->attr('name');
}
$rows = array();
$col_count = count($headers);
foreach ($qp
->top()
->find('results>result') as $row) {
$cols = array();
$row
->children();
for ($i = 0; $i < $col_count; ++$i) {
$item = $row
->branch()
->eq($i);
if ($item
->branch()
->children('uri')
->size() == 1) {
$txt = $item
->text();
$cols[$i] = querypath_examples_format_ld_link($txt, $txt);
}
else {
$cols[$i] = $item
->text();
}
}
$rows[] = $cols;
}
$caption = t('SPARQL Query results from DBPedia');
$out = t('Results of issuing @url the following query: ', array(
'@url' => $base_url,
)) . '<pre>' . $sparql . '</pre>';
$out .= theme('table', $headers, $rows, array(), $caption);
$out .= htmlentities($qp
->top()
->xml());
return $out;
}