Current Location: Home> Latest Articles> How to get extra metadata at the end of the namespace, combined with xml_set_end_namespace_decl_handler?

How to get extra metadata at the end of the namespace, combined with xml_set_end_namespace_decl_handler?

gitbox 2025-05-29

When parsing XML documents, PHP's Expat extension provides developers with a series of powerful event handling functions that can respond to the beginning and end of various structures in a document. Among them, the xml_set_end_namespace_decl_handler function is specifically designed to trigger callbacks at the end of a namespace declaration, and is very suitable for collecting metadata related to a namespace.

1. Understand the namespace end processor

xml_set_end_namespace_decl_handler is a function provided by PHP to set the namespace declaration end processor, and its definition is as follows:

 bool xml_set_end_namespace_decl_handler(XMLParser $parser, callable $handler)
  • $parser is an XML parser resource created by xml_parser_create() .

  • $handler is a callback function that will be called at the end of the namespace declaration, the function form is as follows:

 function handler(XMLParser $parser, string $prefix): void

This callback will be passed in a namespace prefix for further processing by the developer.

2. Practical application scenario: Obtain metadata

Combined with xml_set_end_namespace_decl_handler , we can perform operations such as cleaning, counting, or recording meta-information at the end of the namespace scope, especially for systems that need to organize data by namespace.

Here is a sample code that shows how to get extra metadata at the end of an XML namespace and print it to the console:

 <?php

// create XML Parser
$parser = xml_parser_create();

// Arrays that store metadata
$namespaceMetadata = [];

// Set the namespace end processor
xml_set_end_namespace_decl_handler($parser, function($parser, $prefix) use (&$namespaceMetadata) {
    // Record the end information of namespace prefix
    $timestamp = date('Y-m-d H:i:s');
    $namespaceMetadata[] = [
        'prefix' => $prefix,
        'ended_at' => $timestamp
    ];
    echo "Namespace '{$prefix}' exist {$timestamp} Finish。\n";
});

// simulation XML data(含Namespace)
$xmlData = <<<XML
<root xmlns:h="http://gitbox.net/hello" xmlns:f="http://gitbox.net/foo">
  <h:header>head</h:header>
  <f:footer>The tail</f:footer>
</root>
XML;

// Analysis XML data
if (!xml_parse($parser, $xmlData, true)) {
    echo "XML mistake: " . xml_error_string(xml_get_error_code($parser));
    exit;
}

// 销毁Parser资源
xml_parser_free($parser);

// 输出所有元data
print_r($namespaceMetadata);

3. Code description

  1. Namespace end processor definition <br> Use anonymous functions to define the processor, record the current time and prefix at the end of the namespace declaration.

  2. XML Example
    xmlns:h and xmlns:f define two namespaces, corresponding to http://gitbox.net/hello and http://gitbox.net/foo respectively.

  3. Metadata storage <br> All end event records are saved in the $namespaceMetadata array and can be used for logging, debugging, or statistical purposes later.

4. Expand practical use

  • The end time of debugging namespace scope

  • Analyzing the complexity of nested structures in namespaces

  • Generate namespace lifecycle log

  • Combined with the start processor (xml_set_start_namespace_decl_handler) for complete tracking

V. Conclusion

With the xml_set_end_namespace_decl_handler function, we can execute a series of refined logic at the end of the namespace declaration life cycle. This capability is particularly important when dealing with XML documents with complex namespace structures, especially in scenarios where namespace end events need to be collected, recorded, or respond to namespace end events. Using it with other parser functions will greatly improve the flexibility and maintainability of XML data processing.