From 5a29c84326c1859dabc42be9721d57a7e4f63384 Mon Sep 17 00:00:00 2001
From: Cloud8 <4reading.rocks@gmail.com>
Date: Mon, 8 Dec 2014 10:42:36 -0500
Subject: [PATCH] Simpler solution to grafting SimpleXML trees.

---
 module/VuFind/src/VuFind/SimpleXML.php        | 58 +++++--------------
 .../src/VuFindTest/SimpleXMLTest.php          |  4 +-
 2 files changed, 18 insertions(+), 44 deletions(-)

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