PHP xml_set_end_namespace_decl_handler() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
XML Parser

Definition

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.

PHP
xml_set_end_namespace_decl_handler($parser, function (XMLParser $parser, string|false $prefix) {
    echo 'closed ', $prefix === false ? '(default)' : $prefix, PHP_EOL;
});
CallWhat it does
xml_set_end_namespace_decl_handler($parser, 'onNsEnd')Uses the global function onNsEnd() when a namespace scope ends.
xml_set_end_namespace_decl_handler($parser, [$reader, 'nsEnd'])Uses the nsEnd() method of the $reader object.
xml_set_end_namespace_decl_handler($parser, fn ($p, $prefix) => print($prefix))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.

Library declarations
PHP
xml_set_end_namespace_decl_handler(XMLParser $parser, callable $handler): true

Parameters

ParameterDescription
parserRequired. An XMLParser object created with xml_parser_create_ns().
handlerRequired. A callable that accepts the XMLParser as its first argument and the namespace prefix as its second argument.

Return Value

Return ValueDescription
trueAlways returns true after the handler is stored.

PHP Examples

Example 1 — Report a Closed Namespace

Create a namespace-aware parser and print the prefix when its declaration goes out of scope.

PHP
<?php
$xml = '<root xmlns:a="urn:a"><a:item>x</a:item></root>';

$parser = xml_parser_create_ns();

xml_set_end_namespace_decl_handler(
    $parser,
    function (XMLParser $p, string|false $prefix) {
        echo 'Closed prefix: ', $prefix, PHP_EOL;
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. xml_parser_create_ns() makes a parser that understands XML namespaces.
  2. The root element declares the prefix a for the URI urn:a.
  3. The scope of that declaration ends when the root element closes.
  4. At that moment the parser calls the handler with the prefix a.

Example 2 — Nested Scopes Close in Reverse

When a child element declares its own prefix, the child prefix closes before the parent prefix. Log both start and end events to see the order.

PHP
<?php
$xml = '<root xmlns:a="urn:a"><child xmlns:b="urn:b"/></root>';

$events = [];
$parser = xml_parser_create_ns();

xml_set_start_namespace_decl_handler(
    $parser,
    function (XMLParser $p, string|false $prefix, string $uri) use (&$events) {
        $events[] = 'start ' . $prefix;
    }
);

xml_set_end_namespace_decl_handler(
    $parser,
    function (XMLParser $p, string|false $prefix) use (&$events) {
        $events[] = 'end ' . $prefix;
    }
);

xml_parse($parser, $xml, true);

echo implode(PHP_EOL, $events), PHP_EOL;

How It Works

  1. The root element declares prefix a and the child element declares prefix b.
  2. The start handler logs a first and b second, because the root opens before its child.
  3. The child closes first, so the end handler logs b before a.
  4. The log shows that namespace scopes nest just like elements.

Example 3 — The Default Namespace Has No Prefix

A default namespace such as xmlns="urn:feed" has no prefix, so the handler receives false instead of a string.

PHP
<?php
$xml = '<feed xmlns="urn:feed"><entry/></feed>';

$parser = xml_parser_create_ns();

xml_set_start_namespace_decl_handler(
    $parser,
    function (XMLParser $p, string|false $prefix, string $uri) {
        echo 'Open: ', $prefix === false ? '(default)' : $prefix, ' = ', $uri, PHP_EOL;
    }
);

xml_set_end_namespace_decl_handler(
    $parser,
    function (XMLParser $p, string|false $prefix) {
        echo 'Close: ', $prefix === false ? '(default)' : $prefix, PHP_EOL;
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. The feed element declares a default namespace with xmlns and no prefix.
  2. The start handler receives false as the prefix, which the code prints as (default).
  3. The entry element closes first and has no declaration of its own.
  4. 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.

Next: xml_set_external_entity_ref_handler()

xml_set_external_entity_ref_handler() registers the function that runs when an XML documen.

xml_set_external_entity_ref_handler() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful