function rdf_entity_to_xml in Taxonomy import/export via XML 7
Given a D7 entity with its 'rdf_mapping' available, create and fill in data on an RDF/XML node in an XML document.
Fully supports namespaces, even though internally the RDF mapping only uses CURIE notation.
Parameters
object $entity: a Drupal entity. Should contain $entity->rdf_mapping instructions.
DOMDocument $XMLDoc: handle on an XML Document to build the element within.
DOMElement|NULL $domcontainer: element to add this structure to. While it's not required, allowing the DOM constructor here to add early will assist in reducing namespace clutter!
Return value
DOMElement an XML node containing the data. Does not actually add the element to the DOM
3 calls to rdf_entity_to_xml()
- taxonomy_xml_rdf_add_terms in formats/
rdf_format.inc - Append definitions of a list of terms on to a DOM container.
- taxonomy_xml_rdf_add_vocab in formats/
rdf_format.inc - Create a vocabulary definition (just the def, not its terms) and insert it into the given document element.
- taxonomy_xml_rdf_export_vocabulary in ./
taxonomy_xml_rdf.inc - Return an RDF representation of the requested vocab.
File
- ./
rdf_utils.inc, line 55 - Experimental library for working with RDF within taxonomy_xml
Code
function rdf_entity_to_xml($entity, DOMDocument $XMLDoc, $domcontainer = NULL) {
if (empty($entity->rdf_mapping)) {
trigger_error("No rdf mapping loaded on this entity " . print_r($entity, 1), E_USER_ERROR);
return FALSE;
}
if (empty($XMLDoc)) {
$XMLDoc = new DOMDocument();
}
static $ns;
if (empty($ns)) {
$ns = rdf_get_namespaces();
}
$mapping = $entity->rdf_mapping;
/*
* rdf_mapping = array(
* rdftype => array( 0 => 'skos:Collection'),
* name => array(
* predicates => array(0 =>'rdfs: label'),
* )
* )
*/
$rdftype = reset($mapping['rdftype']);
// Is a soft CURIE, eg 'skos:Collection'
// Need to know the real URI before adding it to a strict DOM.
$ns_id = fully_qualified_from_curie($rdftype);
$entity_node = $XMLDoc
->createElementNS($ns_id['ns'], $rdftype);
if (!$entity_node) {
trigger_error("Failed to create an XML node '{$rdftype}' on the XML document");
return FALSE;
}
if (!empty($domcontainer)) {
$domcontainer
->appendchild($entity_node);
}
foreach ($mapping as $property_name => $mapping_details) {
if (empty($mapping_details['predicates'])) {
continue;
}
// The value of this predicate
// will be the corresponding property on this object.
if (!isset($entity->{$property_name})) {
watchdog('taxonomy_xml', 'Needs work. The rdf mapping wanted to know the value of the property %property_name but the entity did not hve a corresponding value set. ' . __FUNCTION__, array(
'%property_name' => $property_name,
), WATCHDOG_DEBUG);
continue;
}
// Skip some elements for now.
// Some mapping needs special treatment!
// Elements with 'type' rel or rev actually link to resources.
// I THINK the rdf_mapping 'callback' may help here,
// but I've not seen it used correctly yet
// eg
// 'skos:member',
// 'skos:broader',
// 'skos:narrower',
if (!empty($mapping_details['type']) && ($mapping_details['type'] == 'rel' || $mapping_details['type'] == 'rev')) {
continue;
}
// That should have filtered out non-string or numeric values (arrays)
// so we know the value is now useful.
$property_value = trim($entity->{$property_name});
foreach ($mapping_details['predicates'] as $predicate) {
$ns_id = fully_qualified_from_curie($predicate);
if (empty($property_value)) {
continue;
}
if (!is_array($property_value)) {
// Assume it's a list. tighter code.
$property_value = array(
$property_value,
);
}
foreach ($property_value as $property_val) {
$property_node = $entity_node
->appendChild($XMLDoc
->createElementNS($ns_id['ns'], $predicate));
$property_node
->appendChild($XMLDoc
->createTextNode($property_val));
}
}
}
return $entity_node;
}