print "Redland model created\n"; $parser = librdf_new_parser($world, 'rdfxml', 'application/rdf+xml', null); print "Redland parser created\n"; $uri = librdf_new_uri($world, 'file:../data/dc.rdf'); print "Parsing...\n"; librdf_parser_parse_into_model($parser, $uri, $uri, $model); print "Done...\n"; librdf_free_uri($uri); librdf_free_parser($parser); $query = librdf_new_query($world, 'sparql', null, "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c ?d WHERE { ?a dc:title ?c . OPTIONAL { ?a dc:related ?d } }", null); print "Querying for dc:titles:\n"; $results = librdf_model_query_execute($model, $query); $count = 1; while ($results && !librdf_query_results_finished($results)) { print "result {$count}: {\n"; for ($i = 0; $i < librdf_query_results_get_bindings_count($results); $i++) { $val = librdf_query_results_get_binding_value($results, $i); if ($val) { $nval = librdf_node_to_string($val); } else { $nval = '(unbound)'; } print " " . librdf_query_results_get_binding_name($results, $i) . "=" . $nval . "\n"; } print "}\n"; librdf_query_results_next($results); $count++; } if ($results) { print "Returned {$count} results\n"; }
/** * Return the current tuple of bindings. * * The result is an associative array using the binding names as the * indices. * * @return array The current bindings tuple * @throws LibRDF_Error If unable to get the current bindings tuple * @access public */ public function current() { if ($this->isvalid and !librdf_query_results_finished($this->query_results)) { $retarr = array(); $numbindings = librdf_query_results_get_bindings_count($this->query_results); if ($numbindings < 0) { throw new LibRDF_Error("Unable to get number of bindings in result"); } for ($i = 0; $i < $numbindings; $i++) { $key = librdf_query_results_get_binding_name($this->query_results, $i); $value = librdf_query_results_get_binding_value($this->query_results, $i); if (!$key or !$value) { throw new LibRDF_Error("Failed to get current binding {$i}"); } $retarr[$key] = LibRDF_Node::makeNode(librdf_new_node_from_node($value)); } return $retarr; } else { return NULL; } }