You are here

function taxonomy_xml_add_terms_as_rdf in Taxonomy import/export via XML 5

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

Given a list of terms, append definitions of them to the passed DOM container

Following w3c, SUMO and Wordnet examples (tho not any explicit instructions, taxonomy terms are modelled as rdfs:Class objects structured using rdfs: subClassOf statements.

Sample from Wordnet:

<Class rdf:about="http://xmlns.com/wordnet/1.6/Cat"> <label>Cat [ 1 ]</label> <comment>feline mammal usually having thick soft fur and being unable to roar; domestic cats; wildcats</comment> <subClassOf> <Class rdf:about="http://xmlns.com/wordnet/1.6/Feline" /> </subClassOf> </Class>

I'm copying that syntax.

Parameters

$termlist a FLAT array of all terms, internally cross-referenced to: each other defining the tree stucture

1 call to taxonomy_xml_add_terms_as_rdf()
taxonomy_xml_rdf_create in ./rdf_format.inc
Return an XML/RDF document representing this vocab

File

./rdf_format.inc, line 575
Include routines for RDF parsing and taxonomy/term creation.

Code

function taxonomy_xml_add_terms_as_rdf(&$domcontainer, $termlist) {
  if (!$termlist) {
    return;
  }
  $dom = $domcontainer->ownerDocument;
  foreach ($termlist as $term) {
    $termnode = $dom
      ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:Class');
    $termnode
      ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:ID', 'term-' . $term->tid);
    $domcontainer
      ->appendchild($termnode);
    $termnode
      ->appendchild($dom
      ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:label', htmlentities($term->name)));
    if ($term->description) {
      $termnode
        ->appendchild($dom
        ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:comment', htmlentities($term->description)));
    }
    $vocab_ref = $dom
      ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:isDefinedBy');
    $vocab_ref
      ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#vocabulary-' . $term->vid);
    $termnode
      ->appendchild($vocab_ref);
    foreach ((array) taxonomy_get_related($term->tid) as $relatedid => $relatedterm) {
      $related_node = $dom
        ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:seeAlso', htmlentities($relatedterm->name));
      $related_node
        ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#term-' . $relatedid);
      $termnode
        ->appendchild($related_node);
    }
    $synonyms = taxonomy_get_synonyms($term->tid);

    // TODO - figure out the right syntax for synonym
    // I'm using 'equivalentClass' ... although that's really intended for merging different vocabs.
    foreach ((array) $synonyms as $synonymname) {
      $synonymnode = $parent_node = $dom
        ->createelementns(TAXONOMY_XML_OWL_NS, 'owl:equivalentClass', htmlentities($synonymname));
      $termnode
        ->appendchild($synonymnode);
    }
    foreach ((array) $term->parents as $parentid) {
      $parentlist = array();
      if ($parentid) {
        $parentlist[$parentid] = $parent = taxonomy_get_term($parentid);
        $parent_node = $dom
          ->createelementns(TAXONOMY_XML_RDFS_NS, 'rdfs:subClassOf', htmlentities($parent->name));
        $parent_node
          ->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#term-' . $parentid);
        $termnode
          ->appendchild($parent_node);
      }
    }

    # dpm(array('adding term to rdf' => $term));

    #$termnode->appendchild($dom->createcomment(print_r($term, 1)));

    // workaround for large vocabs - extend runtime indefinately
    set_time_limit(10);
  }

  // Done all terms in list
}