The Gitlab instance will be restarted on Monday April 28th at 2AM. There will be a short interruption of service.

Skip to content
Snippets Groups Projects
Commit 5a29c843 authored by Cloud8's avatar Cloud8 Committed by Demian Katz
Browse files

Simpler solution to grafting SimpleXML trees.

parent e46a0d96
Branches
Tags
No related merge requests found
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
* @link http://vufind.org/wiki/vufind2:developer_manual Wiki * @link http://vufind.org/wiki/vufind2:developer_manual Wiki
*/ */
namespace VuFind; namespace VuFind;
use SimpleXMLElement;
/** /**
* VuFind SimpleXML enhancement functionality * VuFind SimpleXML enhancement functionality
...@@ -39,57 +40,28 @@ namespace VuFind; ...@@ -39,57 +40,28 @@ namespace VuFind;
class SimpleXML class SimpleXML
{ {
/** /**
* Attach $child to $parent. Adapted from function defined in PHP docs here: * Attach $child to $parent.
* http://www.php.net/manual/en/class.simplexmlelement.php#99071
* *
* @param SimpleXMLElement $parent Parent element to modify * @param SimpleXMLElement $parent Parent element to modify
* @param SimpleXMLElement $child Child element to attach * @param SimpleXMLElement|string $child Child element (or XML fragment) to
* attach
* *
* @return void * @return void
*/ */
public static function appendElement($parent, $child) public static function appendElement($parent, $child)
{ {
// get all namespaces for document $xml = $child instanceof SimpleXMLElement
$namespaces = $child->getNamespaces(true); ? $child->asXML() : $child;
// check if there is a default namespace for the current node // strip off xml header
$currentNs = $child->getNamespaces(); $mark = strpos($xml,'?'.'>');
$defaultNs = count($currentNs) > 0 ? current($currentNs) : null; if ($mark>0 && $mark<40) {
$prefix = (count($currentNs) > 0) ? current(array_keys($currentNs)) : ''; $xml = substr($xml, $mark + 2);
$childName = strlen($prefix) > 1
? $prefix . ':' . $child->getName() : $child->getName();
// check if the value is string value / data
if (trim((string) $child) == '') {
$element = $parent->addChild($childName, null, $defaultNs);
} else {
$element = $parent->addChild(
$childName, htmlspecialchars((string)$child), $defaultNs
);
}
foreach ($child->attributes() as $attKey => $attValue) {
$element->addAttribute($attKey, $attValue);
}
foreach ($namespaces as $nskey => $nsurl) {
foreach ($child->attributes($nsurl) as $attKey => $attValue) {
$element->addAttribute($nskey . ':' . $attKey, $attValue, $nsurl);
}
} }
// add children -- try with namespaces first, but default to all children $dom = dom_import_simplexml($parent);
// if no namespaced children are found. $fragment = $dom->ownerDocument->createDocumentFragment();
$children = 0; $fragment->appendXML($xml);
foreach ($namespaces as $nskey => $nsurl) { $dom->appendChild($fragment);
foreach ($child->children($nsurl) as $currChild) {
self::appendElement($element, $currChild);
$children++;
}
}
if ($children == 0) {
foreach ($child->children() as $currChild) {
self::appendElement($element, $currChild);
}
}
} }
} }
\ No newline at end of file
...@@ -48,7 +48,9 @@ class SimpleXMLTest extends \PHPUnit_Framework_TestCase ...@@ -48,7 +48,9 @@ class SimpleXMLTest extends \PHPUnit_Framework_TestCase
$parent = simplexml_load_string('<top><children></children></top>'); $parent = simplexml_load_string('<top><children></children></top>');
$child = simplexml_load_string('<child attr="true" />'); $child = simplexml_load_string('<child attr="true" />');
$expected = simplexml_load_string( $expected = simplexml_load_string(
'<top><children><child attr="true" /></children></top>' '<top><children>
<child attr="true" />
</children></top>'
); );
\VuFind\SimpleXML::appendElement($parent->children, $child); \VuFind\SimpleXML::appendElement($parent->children, $child);
$this->assertEquals($expected->asXML(), $parent->asXML()); $this->assertEquals($expected->asXML(), $parent->asXML());
......
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