@@ -37,30 +37,31 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $
3737 {
3838 $ root_item = $ sxml [$ root_item_index ];
3939
40+ // The DOM exposes information which SimpleXML doesn't make easy to find
41+ // Note that this is not an expensive conversion, as we are only swapping PHP wrappers around an existing LibXML resource
42+ $ dom_item = dom_import_simplexml ($ root_item );
43+
44+ // To what namespace does this element or attribute belong?
45+ $ ns_prefix = $ dom_item ->prefix ;
46+
4047 // Special case if the root is actually an attribute
4148 // It's surprisingly hard to find something which behaves consistently differently for an attribute and an element within SimpleXML
42- // The below relies on the fact that the DOM makes a much clearer distinction
43- // Note that this is not an expensive conversion, as we are only swapping PHP wrappers around an existing LibXML resource
44- if ( dom_import_simplexml ($ root_item ) instanceOf DOMAttr )
49+ if ( $ dom_item instanceOf DOMAttr )
4550 {
46- // To what namespace does this attribute belong? Returns array( alias => URI )
47- $ ns = $ root_item ->getNamespaces (false );
48- if ( key ($ ns ) )
51+ if ( $ ns_prefix )
4952 {
50- $ dump .= key ( $ ns ) . ': ' ;
53+ $ dump .= $ ns_prefix . ': ' ;
5154 }
5255 $ dump .= $ root_item ->getName () . '=" ' . (string )$ root_item . '" ' . PHP_EOL ;
5356 }
5457 else
5558 {
5659 // Display the root node as a numeric key reference, plus a hint as to its tag name
5760 // e.g. '[42] // <Answer>'
58-
59- // To what namespace does this attribute belong? Returns array( alias => URI )
60- $ ns = $ root_item ->getNamespaces (false );
61- if ( key ($ ns ) )
61+
62+ if ( $ ns_prefix )
6263 {
63- $ root_node_name = key ( $ ns ) . ': ' . $ root_item ->getName ();
64+ $ root_node_name = $ ns_prefix . ': ' . $ root_item ->getName ();
6465 }
6566 else
6667 {
@@ -123,19 +124,22 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_string
123124 }
124125 }
125126
126- // To what namespace does this element belong? Returns array( alias => URI )
127+ // The DOM exposes information which SimpleXML doesn't make easy to find
128+ // Note that this is not an expensive conversion, as we are only swapping PHP wrappers around an existing LibXML resource
129+ $ dom_item = dom_import_simplexml ($ item );
130+
131+ // To what namespace does this element or attribute belong?
132+
127133 // For top-level elements, cheat, and say they're in the null namespace, to force a ->children() call
128134 if ( $ depth == 1 )
129135 {
130- $ item_ns = array ('' => NULL );
136+ $ item_ns_prefix = '' ;
137+ $ item_ns_uri = null ;
131138 }
132139 else
133140 {
134- $ item_ns = $ item ->getNamespaces (false );
135- if ( !$ item_ns )
136- {
137- $ item_ns = array ('' => NULL );
138- }
141+ $ item_ns_prefix = $ dom_item ->prefix ;
142+ $ item_ns_uri = $ dom_item ->namespaceURI ;
139143 }
140144
141145 // This returns all namespaces used by this node and all its descendants,
@@ -146,18 +150,21 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_string
146150 {
147151 $ all_ns ['' ] = NULL ;
148152 }
149-
153+
150154 // Prioritise "current" namespace by merging into onto the beginning of the list
151155 // (it will be added to the beginning and the duplicate entry dropped)
152- $ all_ns = array_merge ($ item_ns , $ all_ns );
153-
156+ $ all_ns = array_merge (
157+ array ($ item_ns_prefix => $ item_ns_uri ),
158+ $ all_ns
159+ );
160+
154161 foreach ( $ all_ns as $ ns_alias => $ ns_uri )
155162 {
156163 $ children = $ item ->children ($ ns_alias , true );
157164 $ attributes = $ item ->attributes ($ ns_alias , true );
158165
159166 // If things are in the current namespace, display them a bit differently
160- $ is_current_namespace = ( $ ns_uri == reset ( $ item_ns ) );
167+ $ is_current_namespace = ( $ ns_uri == $ item_ns_uri );
161168
162169 $ ns_uri_quoted = (strlen ($ ns_uri ) == 0 ? 'null ' : "' $ ns_uri' " );
163170
0 commit comments