In PHP, xml_set_end_namespace_decl_handler() registers the function that runs when an XML namespace declaration goes out of scope.
A namespace declaration such as xmlns:a="urn:a" is valid for the element that contains it and everything inside. When that element closes, the parser calls this handler with the prefix. It only works with a parser created by xml_parser_create_ns().
Syntax
Call xml_set_end_namespace_decl_handler() with the values you need. Start with this line.
Uses a short arrow function that prints the prefix.
Your handler is called as handler(XMLParser $parser, string|false $prefix). The prefix is false for a default namespace such as xmlns="urn:feed". This is a niche handler, and everyday XML apps mainly need the element handler and the character data handler.
The feed element declares a default namespace with xmlns and no prefix.
The start handler receives false as the prefix, which the code prints as (default).
The entry element closes first and has no declaration of its own.
When the feed element closes, the end handler also receives false and prints Close: (default).
Frequently Asked Questions
It sets the function the parser calls when a namespace declaration goes out of scope, which happens when the element that declared it is closed.
The parser must come from xml_parser_create_ns(). A parser from xml_parser_create() treats xmlns attributes as ordinary attributes and never calls namespace handlers.
The handler receives false for a default namespace such as xmlns="urn:feed". Check with === false before using the value as a string.
No. Most scripts only need xml_set_element_handler() and xml_set_character_data_handler(). Namespace handlers are for tools that must track prefixes.
Use xml_set_start_namespace_decl_handler(). It receives both the prefix and the URI when the declaration opens, so you can store them and look them up later.