Skip to content
Snippets Groups Projects
Commit 866cd734 authored by Demian Katz's avatar Demian Katz
Browse files

Improved namespace handling.

parent 9cebd6ef
Branches
Tags
No related merge requests found
...@@ -564,7 +564,8 @@ class OAI ...@@ -564,7 +564,8 @@ class OAI
// there is probably a cleaner way to do this, but this simple method avoids // there is probably a cleaner way to do this, but this simple method avoids
// the complexity of dealing with namespaces in SimpleXML: // the complexity of dealing with namespaces in SimpleXML:
$xml = trim($record->metadata->asXML()); $xml = trim($record->metadata->asXML());
$xml = preg_replace('/(^<metadata>)|(<\/metadata>$)/m', '', $xml); preg_match('/^<metadata([^\>]*)>/', $xml, $extractedNs);
$xml = preg_replace('/(^<metadata[^\>]*>)|(<\/metadata>$)/m', '', $xml);
// If we are supposed to inject any values, do so now inside the first // If we are supposed to inject any values, do so now inside the first
// tag of the file: // tag of the file:
...@@ -607,11 +608,41 @@ class OAI ...@@ -607,11 +608,41 @@ class OAI
if (!empty($insert)) { if (!empty($insert)) {
$xml = preg_replace('/>/', '>' . $insert, $xml, 1); $xml = preg_replace('/>/', '>' . $insert, $xml, 1);
} }
$xml = $this->fixNamespaces(
$xml, $record->getDocNamespaces(),
isset($extractedNs[1]) ? $extractedNs[1] : ''
);
// Save our XML: // Save our XML:
file_put_contents($this->getFilename($id, 'xml'), trim($xml)); file_put_contents($this->getFilename($id, 'xml'), trim($xml));
} }
/**
* Support method for saveRecord() -- fix namespaces in the top tag of the XML
* document to compensate for bugs in the SimpleXML library.
*
* @param string $xml XML document to clean up
* @param array $ns Namespaces to check
* @param string $attr Attributes extracted from the <metadata> tag
*
* @return string
*/
protected function fixNamespaces($xml, $ns, $attr = '')
{
foreach ($ns as $key => $val) {
if (!empty($key)
&& strstr($xml, $key . ':') && !strstr($xml, 'xmlns:' . $key)
&& !strstr($attr, 'xmlns:' . $key)
) {
$attr .= ' xmlns:' . $key . '="' . $val . '"';
}
}
if (!empty($attr)) {
$xml = preg_replace('/>/', $attr . '>', $xml, 1);
}
return $xml;
}
/** /**
* Load date granularity from the server. * Load date granularity from the server.
* *
...@@ -668,7 +699,7 @@ class OAI ...@@ -668,7 +699,7 @@ class OAI
} }
/** /**
* Extract the ID from a record object (support method for _processRecords()). * Extract the ID from a record object (support method for processRecords()).
* *
* @param object $record SimpleXML record. * @param object $record SimpleXML record.
* *
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment