From 6ac7c40043364bfaa48672687d706047c173e79f Mon Sep 17 00:00:00 2001 From: Rowan Collins Date: Thu, 9 Nov 2017 21:07:32 +0000 Subject: [PATCH] Fix #4: extra ->children() should show for prefixed root element --- src/simplexml_tree.php | 16 +++++++++++---- tests/tree-output/basic-default-ns | 33 +++++++++++++++--------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/simplexml_tree.php b/src/simplexml_tree.php index 4348d2f..094e612 100644 --- a/src/simplexml_tree.php +++ b/src/simplexml_tree.php @@ -122,14 +122,22 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_string . PHP_EOL; } } - + // To what namespace does this element belong? Returns array( alias => URI ) - $item_ns = $item->getNamespaces(false); - if ( ! $item_ns ) + // For top-level elements, cheat, and say they're in the null namespace, to force a ->children() call + if ( $depth == 1 ) { $item_ns = array('' => NULL); } - + else + { + $item_ns = $item->getNamespaces(false); + if ( !$item_ns ) + { + $item_ns = array('' => NULL); + } + } + // This returns all namespaces used by this node and all its descendants, // whether declared in this node, in its ancestors, or in its descendants $all_ns = $item->getNamespaces(true); diff --git a/tests/tree-output/basic-default-ns b/tests/tree-output/basic-default-ns index 69be6c0..dc26004 100644 --- a/tests/tree-output/basic-default-ns +++ b/tests/tree-output/basic-default-ns @@ -1,18 +1,19 @@ SimpleXML object (1 item) [0] // - ->movie[0] - ->title[0] - ->characters[0] - ->character[0] - ->name[0] - ->actor[0] - ->character[1] - ->name[0] - ->actor[0] - ->plot[0] - ->great-lines[0] - ->line[0] - ->rating[0] - ['type'] - ->rating[1] - ['type'] + ->children('', true) + ->movie[0] + ->title[0] + ->characters[0] + ->character[0] + ->name[0] + ->actor[0] + ->character[1] + ->name[0] + ->actor[0] + ->plot[0] + ->great-lines[0] + ->line[0] + ->rating[0] + ['type'] + ->rating[1] + ['type']