You are here

function taxonomy_xml_rdf_create in Taxonomy import/export via XML 7

Same name and namespace in other branches
  1. 5.2 rdf_format.inc \taxonomy_xml_rdf_create()
  2. 5 rdf_format.inc \taxonomy_xml_rdf_create()
  3. 6.2 rdf_format.inc \taxonomy_xml_rdf_create()
  4. 6 rdf_format.inc \taxonomy_xml_rdf_create()

Return an XML/RDF document representing this vocab

I'd like to use ARC libraries, but it doesn't appear to include an RDF serializer output method, only an input parser...

Uses PHP DOM to create DOM document and nodes.

We use namespaces carefully here, although it may create wordy output if the DOM is not optimizing the declarations for us. Still, best to be explicit, it would seem.

The URI used to refer to other resources is based on the source document location, eg http://this.server/taxonomy/vocabulary/{vid}/rdf#{tid}

Preamble should look something like:

<rdf:RDF xmlns:rdf ="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns: rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns: owl="http://www.w3.org/2002/07/owl#"

Return value

string An XML document string.

File

formats/rdf_format.inc, line 700

Code

function taxonomy_xml_rdf_create($vocabulary, $parent = 0, $depth = -1, $max_depth = NULL) {
  $vocabulary = is_numeric($vocabulary) ? taxonomy_vocabulary_load($vocabulary) : $vocabulary;
  $domcontainer = taxonomy_xml_rdf_document();
  $dom = $domcontainer->ownerDocument;

  // Define the vocab.
  taxonomy_xml_rdf_add_vocab($domcontainer, $vocabulary);

  // Now start adding terms.
  // They are listed as siblings, not children of the ontology.
  $tree = taxonomy_get_tree($vocabulary->vid, $parent, $max_depth, $depth);
  taxonomy_xml_rdf_add_terms($domcontainer, $tree);
  $result = $dom
    ->savexml();

  // Minor layout tweak for readability,
  // singletons go on their own lines.
  $result = preg_replace('|(<[^<]*/>)|', "\$1\n", $result);

  // Nested tags go onto new lines.
  $result = preg_replace('|><|', ">\n<", $result);
  return $result;
}