You are here

public function EasyRdf_Serialiser_RdfXml::serialise in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/RdfXml.php \EasyRdf_Serialiser_RdfXml::serialise()

Method to serialise an EasyRdf_Graph to RDF/XML

Parameters

EasyRdf_Graph $graph An EasyRdf_Graph object.:

string $format The name of the format to convert to.:

array $options:

Return value

string The RDF in the new desired format.

Throws

EasyRdf_Exception

Overrides EasyRdf_Serialiser::serialise

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/RdfXml.php, line 196

Class

EasyRdf_Serialiser_RdfXml
Class to serialise an EasyRdf_Graph to RDF/XML with no external dependancies.

Code

public function serialise($graph, $format, array $options = array()) {
  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 = '';

  // Serialise URIs first
  foreach ($graph
    ->resources() as $resource) {
    if (!$resource
      ->isBnode()) {
      $xml .= $this
        ->rdfxmlResource($resource, true);
    }
  }

  // Serialise bnodes afterwards
  foreach ($graph
    ->resources() as $resource) {
    if ($resource
      ->isBnode()) {
      $xml .= $this
        ->rdfxmlResource($resource, true);
    }
  }

  // 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        ";
    }
    if (strlen($prefix) === 0) {
      $namespaceStr .= ' xmlns="' . htmlspecialchars($url) . '"';
    }
    else {
      $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"';
    }
  }
  return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n";
}