Method to serialise an EasyRdf_Graph to RDF/XML
object EasyRdf_Graph $graph An EasyRdf_Graph object.:
string $format The name of the format to convert to.:
string The RDF in the new desired format.
Overrides EasyRdf_Serialiser::serialise
public function serialise($graph, $format) {
parent::checkSerialiseParams($graph, $format);
if ($format != 'rdfxml') {
throw new EasyRdf_Exception("EasyRdf_Serialiser_RdfXml does not support: {$format}");
}
// store of namespaces to be appended to the rdf:RDF tag
$this->prefixes = array(
'rdf' => true,
);
// store of the resource URIs we have serialised
$this->outputtedResources = array();
$xml = '';
foreach ($graph
->resources() as $resource) {
$xml .= $this
->rdfxmlResource($resource, true, 1);
}
// iterate through namepsaces array prefix and output a string.
$namespaceStr = '';
foreach ($this->prefixes as $prefix => $count) {
$url = EasyRdf_Namespace::get($prefix);
if (strlen($namespaceStr)) {
$namespaceStr .= "\n ";
}
$namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"';
}
return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n";
}