When processing XML data, especially documents with namespaces, using PHP's XML parser (based on Expat) is a flexible and efficient way to do it. This article will focus on explaining the use of the xml_set_end_namespace_decl_handler function, and combine it with complete examples to illustrate how to correctly utilize the function and handle the end of the namespace declaration during the parsing process.
PHP provides a series of event-driven XML parsing functions, such as xml_parser_create , xml_parse , xml_set_element_handler , etc. In namespace-related processing, xml_set_start_namespace_decl_handler and xml_set_end_namespace_decl_handler correspond to the namespace declaration start and end respectively.
Although the processing of starting a namespace is more commonly used, ending processing is equally important, especially when nesting is complex or when the scope stack needs to be maintained. With xml_set_end_namespace_decl_handler , we can perform cleanup operations at the end of the namespace, maintain the namespace stack, or log logs for debugging.
bool xml_set_end_namespace_decl_handler ( resource $parser , callable $handler )
$parser : A parser resource created by xml_parser_create .
$handler : The callback function, in the form of function handler(resource $parser, string $prefix) , is called at the end of the namespace declaration.
Here is a complete example using xml_set_end_namespace_decl_handler :
<?php
$xml = <<<XML
<?xml version="1.0"?>
<root xmlns:h="http://gitbox.net/hello" xmlns:f="http://gitbox.net/foo">
<h:child>Content</h:child>
<f:child>Another</f:child>
</root>
XML;
$parser = xml_parser_create();
// Set namespace processing
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
// Namespace starts declaring processor
xml_set_start_namespace_decl_handler($parser, function($parser, $prefix, $uri) {
echo "Namespace Start: prefix = {$prefix}, uri = {$uri}\n";
});
// Namespace end declaration processor
xml_set_end_namespace_decl_handler($parser, function($parser, $prefix) {
echo "Namespace End: prefix = {$prefix}\n";
});
// Element processor
xml_set_element_handler($parser,
function($parser, $name, $attrs) {
echo "Start Element: {$name}\n";
},
function($parser, $name) {
echo "End Element: {$name}\n";
}
);
// Execute parsing
if (!xml_parse($parser, $xml, true)) {
$error = xml_error_string(xml_get_error_code($parser));
$line = xml_get_current_line_number($parser);
die("XML Error: {$error} at line {$line}\n");
}
xml_parser_free($parser);
Run the above script and output the following:
Namespace Start: prefix = h, uri = http://gitbox.net/hello
Namespace Start: prefix = f, uri = http://gitbox.net/foo
Start Element: root
Start Element: h:child
End Element: h:child
Start Element: f:child
End Element: f:child
End Element: root
Namespace End: prefix = f
Namespace End: prefix = h
From the output, we can see:
The order of namespace declarations is consistent with their definitions in the document;
After the element parsing is completed, the xml_set_end_namespace_decl_handler correctly captures the end of the namespace;
If you maintain the namespace scope stack during parsing, this is a good time to release the stack.
Scope control : When you use namespace scopes in XML parsing (such as configuring parsers or building tree structures), be sure to use both start and end namespace declaration processors.
Debugging tools : When debugging complex situations such as namespace conflicts, nesting and mixing, using xml_set_end_namespace_decl_handler can help you better understand the structure of XML documents.
Namespace stack design : You can use PHP array to simulate the stack structure, put it on the start and out the stack when end , so as to more accurately track the current namespace context.
Although xml_set_end_namespace_decl_handler is not commonly used as element processors, it can provide a very critical auxiliary role when dealing with complex XML, especially when mixing multiple namespaces. Mastering its usage can make your XML parser stronger and more robust. Hopefully this article helps you make the most of this powerful tool function when parsing XML using PHP.