function taxonomy_xml_entity_to_rdf in Taxonomy import/export via XML 6.2
Given a Drupal object, some mapping rules and a DOMDocument, create the XML representationof the thing
This should have been in the RDF project from day 1, but instead I'll invent it today, here.
Return value
a DOMNode
See also
rdf_mapping project
2 calls to taxonomy_xml_entity_to_rdf()
- taxonomy_xml_add_terms_as_rdf in ./
rdf_format.inc - Given a list of terms, append definitions of them to the passed DOM container
- taxonomy_xml_add_vocab_as_rdf in ./
rdf_format.inc - Create a vocabulary definition (just the def, not its terms) and insert it into the given document element.
File
- ./
rdf_format.inc, line 1176 - Include routines for RDF parsing and taxonomy/term creation. @author dman http://coders.co.nz
Code
function taxonomy_xml_entity_to_rdf($object, $object_type, $domcontainer) {
$dom = $domcontainer->ownerDocument;
// Get the mapping rules for rdf schema, D7 style
$mapping = taxonomy_xml_get_mapping($object_type);
// What is the rdf type we use to describe this type of thing (eg 'skos:ConceptScheme')
$object_type_curie = array_pop($mapping['rdftype']);
$object_type_full = taxonomy_xml_parse_curie($object_type_curie);
// Describe the thing itself, create a DOMNode
$object_node = $dom
->createelementns($object_type_full['uri'], $object_type_full['id']);
// Add it to the document immediately so it can inherit the xmlns declarations and not re-invent them
$domcontainer
->appendchild($object_node);
// Map everything that has a matching attribute to an RDF element of the appropriate name
foreach ($mapping as $drupal_attribute => $attribute_mapping) {
if (!empty($object->{$drupal_attribute})) {
// TODO - using isset made a load of empty things, but will empty() bork on zero?
foreach ($attribute_mapping['predicates'] as $predicate_curie) {
$predicate_full = taxonomy_xml_parse_curie($predicate_curie);
$data = $object->{$drupal_attribute};
// The data may be an array. Some fields can be multiple values.
// Assume it always is, that's easier than switching
if (!is_array($data)) {
$data = array(
$data,
);
}
foreach ($data as $datum) {
// May need to transform the data a little. Often to unpack internal IDs into portable ones
if (isset($attribute_mapping['callback']) && function_exists($attribute_mapping['callback'])) {
$callback = $attribute_mapping['callback'];
$datum = $callback($datum, '#');
// The callback may return a structured URI if a rel type was asked for
// But normally it's just cooked data.
}
// 'rel' data becomes RDF URI links.
if (isset($attribute_mapping['type']) && $attribute_mapping['type'] == 'rel') {
$rel_node = $dom
->createelementns($predicate_full['uri'], $predicate_full['id']);
if (is_array($datum)) {
if (isset($datum['id'])) {
$rel_node
->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', '#' . $datum['id']);
}
else {
if (isset($datum['path'])) {
$rel_node
->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', url($datum['path']));
}
}
// This was handy for humans, but redundant in the graph
//if (isset($datum['title'])) {
// $rel_node->setattributens(TAXONOMY_XML_RDFS_NS, 'rdfs:title', xmlentities($datum['title']) );
//}
}
else {
$rel_node
->setattributens(TAXONOMY_XML_RDF_NS, 'rdf:resource', xmlentities(trim($datum)));
}
$object_node
->appendchild($rel_node);
}
else {
// Normal content
$object_node
->appendchild($dom
->createelementns($predicate_full['uri'], $predicate_full['id'], xmlentities(trim($datum))));
}
}
// each attribute value
}
// each predicate
}
// attribute is set
}
// each mapping
return $object_node;
}