Skip to content

Commit

Permalink
Fix bug while collect namespaces
Browse files Browse the repository at this point in the history
Fix bug while collect namespaces for attributes
  • Loading branch information
JackieDo committed Nov 26, 2018
1 parent 8396d14 commit c406475
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions src/Xml2Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,24 @@ public function convertFrom($inputXml)
$this->loadXml($inputXml);

// Convert the XML to an array, starting with the root node
$rootNode = $this->xml->documentElement->nodeName;
$this->array[$rootNode] = $this->parseNode($this->xml->documentElement);
$rootNode = $this->xml->documentElement;
$rootValue = $this->parseNode($rootNode);
$rootNodeName = $rootNode->nodeName;

$this->array[$rootNodeName] = $rootValue;

// Add namespacing information to the root node
if (!empty($this->namespaces) && $this->config['namespacesOnRoot']) {
if (!isset($this->array[$rootNode][$this->config['attributesKey']])) {
$this->array[$rootNode][$this->config['attributesKey']] = [];
if (!isset($this->array[$rootNodeName][$this->config['attributesKey']])) {
$this->array[$rootNodeName][$this->config['attributesKey']] = [];
}

foreach ($this->namespaces as $uri => $prefix) {
if ($prefix) {
$prefix = self::ATTRIBUTE_NAMESPACE_SEPARATOR . $prefix;
}

$this->array[$rootNode][$this->config['attributesKey']][self::ATTRIBUTE_NAMESPACE . $prefix] = $uri;
$this->array[$rootNodeName][$this->config['attributesKey']][self::ATTRIBUTE_NAMESPACE . $prefix] = $uri;
}
}

Expand Down Expand Up @@ -186,6 +189,7 @@ public function toJson($options = 0)
protected function loadXml($inputXml)
{
$this->xml = new DOMDocument($this->config['version'], $this->config['encoding']);
// $this->xml->preserveWhiteSpace = false;

if (is_string($inputXml)) {
$this->xml->loadXML($inputXml);
Expand All @@ -208,7 +212,7 @@ protected function loadXml($inputXml)
protected function parseNode(DOMNode $node)
{
$output = [];
$output = $this->collectNamespaces($node, $output);
$output = $this->collectNodeNamespaces($node, $output);

switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
Expand Down Expand Up @@ -316,18 +320,45 @@ protected function collectAttributes(DOMNode $node, $output)
}

$attributes = [];
$namespaces = [];

foreach ($node->attributes as $attributeName => $attributeNode) {
$attributeName = $attributeNode->nodeName;
$attributes[$attributeName] = (string) $attributeNode->value;

if ($attributeNode->namespaceURI) {
$nsUri = $attributeNode->namespaceURI;
$nsPrefix = $attributeNode->lookupPrefix($nsUri);

$namespaces = $this->collectNamespaces($attributeNode);
}
}

// if its a leaf node, store the value in @value instead of directly it.
if (!is_array($output)) {
$output = [$this->config['valueKey'] => $output];
}

$output[$this->config['attributesKey']] = $attributes;
$output[$this->config['attributesKey']] = array_merge($attributes, $namespaces);

return $output;
}

/**
* Collect namespaces for special DOMNode
*
* @param DOMNode $node
* @param array $output
*
* @return array
*/
protected function collectNodeNamespaces(DOMNode $node, array $output)
{
$namespaces = $this->collectNamespaces($node);

if (!empty($namespaces)) {
$output[$this->config['attributesKey']] = $namespaces;
}

return $output;
}
Expand All @@ -336,12 +367,13 @@ protected function collectAttributes(DOMNode $node, $output)
* Get the namespace of the supplied node, and add it to the list of known namespaces for this document
*
* @param DOMNode $node
* @param mixed $output
*
* @return mixed
*/
protected function collectNamespaces(DOMNode $node, $output)
protected function collectNamespaces(DOMNode $node)
{
$namespaces = [];

if ($node->namespaceURI) {
$nsUri = $node->namespaceURI;
$nsPrefix = $node->lookupPrefix($nsUri);
Expand All @@ -354,11 +386,11 @@ protected function collectNamespaces(DOMNode $node, $output)
$nsPrefix = self::ATTRIBUTE_NAMESPACE_SEPARATOR . $nsPrefix;
}

$output[$this->config['attributesKey']][self::ATTRIBUTE_NAMESPACE . $nsPrefix] = $nsUri;
$namespaces[self::ATTRIBUTE_NAMESPACE . $nsPrefix] = $nsUri;
}
}
}

return $output;
return $namespaces;
}
}

0 comments on commit c406475

Please sign in to comment.