Skip to content

Commit 6ac7c40

Browse files
committed
Fix #4: extra ->children() should show for prefixed root element
1 parent f3238d9 commit 6ac7c40

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/simplexml_tree.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,22 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_string
122122
. PHP_EOL;
123123
}
124124
}
125-
125+
126126
// To what namespace does this element belong? Returns array( alias => URI )
127-
$item_ns = $item->getNamespaces(false);
128-
if ( ! $item_ns )
127+
// For top-level elements, cheat, and say they're in the null namespace, to force a ->children() call
128+
if ( $depth == 1 )
129129
{
130130
$item_ns = array('' => NULL);
131131
}
132-
132+
else
133+
{
134+
$item_ns = $item->getNamespaces(false);
135+
if ( !$item_ns )
136+
{
137+
$item_ns = array('' => NULL);
138+
}
139+
}
140+
133141
// This returns all namespaces used by this node and all its descendants,
134142
// whether declared in this node, in its ancestors, or in its descendants
135143
$all_ns = $item->getNamespaces(true);

tests/tree-output/basic-default-ns

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
SimpleXML object (1 item)
22
[0] // <movies>
3-
->movie[0]
4-
->title[0]
5-
->characters[0]
6-
->character[0]
7-
->name[0]
8-
->actor[0]
9-
->character[1]
10-
->name[0]
11-
->actor[0]
12-
->plot[0]
13-
->great-lines[0]
14-
->line[0]
15-
->rating[0]
16-
['type']
17-
->rating[1]
18-
['type']
3+
->children('', true)
4+
->movie[0]
5+
->title[0]
6+
->characters[0]
7+
->character[0]
8+
->name[0]
9+
->actor[0]
10+
->character[1]
11+
->name[0]
12+
->actor[0]
13+
->plot[0]
14+
->great-lines[0]
15+
->line[0]
16+
->rating[0]
17+
['type']
18+
->rating[1]
19+
['type']

0 commit comments

Comments
 (0)