function query_to_string($query, $format, $base_uri = null) { timings($query); if (is_null($base_uri)) { $base_uri = $this->_base_uri; } elseif (is_string($base_uri)) { $base_uri = librdf_new_uri($this->_world, $base_uri); } $q = librdf_new_query($this->_world, 'sparql', null, $query, $base_uri); $r = librdf_model_query_execute($this->_model, $q); if (in_array($format, array('csv', 'tsv'))) { $format = 'http://www.w3.org/ns/formats/SPARQL_Results_' . strtoupper($format); } else { $format = 'http://www.w3.org/2001/sw/DataAccess/json-sparql/'; } $format_uri = librdf_new_uri($this->_world, $format); if ($r) { $r = librdf_query_results_to_string($r, $format_uri, $base_uri); } librdf_free_query($q); librdf_free_uri($format_uri); timings(); return $r; }
$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"; } $results = null; print "\nExecuting query again\n"; $results = librdf_model_query_execute($model, $query); if ($results) { $str = librdf_query_results_to_string($results, null, null); print "Query results serialized to an XML string size " . strlen($str) . " bytes\n"; } else { print "Query results couldn't be serialized to an XML string\n"; } $serializer = librdf_new_serializer($world, 'rdfxml', null, null); print "Redland serializer created\n"; $base = librdf_new_uri($world, 'http://example.org/base.rdf'); print "Serializing...\n"; librdf_serializer_serialize_model_to_file($serializer, './test-out.rdf', $base, $model); print "Done...\n"; librdf_free_serializer($serializer); librdf_free_uri($base); librdf_free_model($model); librdf_free_storage($storage); print "Done\n";
/** * Serialize the results to a string. * * @param string $uri The uri of the target syntax or NULL * @param string $base_uri The base URI for the output or NULL * @return string The results as a string * @throws LibRDF_Error If unable to create a string from the results * @access public */ public function to_string($uri = NULL, $base_uri = NULL) { if ($uri) { $uri = new LibRDF_URI($uri); } if ($base_uri) { $base_uri = new LibRDF_URI($base_uri); } $ret = librdf_query_results_to_string($this->query_results, $uri ? $uri->getURI() : NULL, $base_uri ? $base_uri->getURI() : NULL); if ($ret) { return $ret; } else { throw new LibRDF_Error("Unable to convert the query results to a string"); } }