PHP xml_set_start_namespace_decl_handler() Function



The PHP XML Parser xml_set_start_namespace_decl_handler() function is used to define a custom handler for the start of namespace declarations in an XML document.

It is part of the PHP XML Parser extension and is enabled when a new namespace prefix is added in the XML. This allows you to process or react to namespace information dynamically while parsing. The function needs an XML parser resource and a callback function to handle the namespace events. It makes simpler to handle XML documents that use namespaces properly.

Syntax

Below is the syntax of the PHP XML Parser xml_set_start_namespace_decl_handler() function −

true xml_set_start_namespace_decl_handler ( XMLParser $xml_parser, callable|string|null $handler )

Parameters

Here are the parameters of the xml_set_start_namespace_decl_handler() function −

  • $xml_parser: (Required) It is a reference to the XML parser to use inside the object.

  • $handler: (Required) It is used to specifies name of function that must exist when xml_parse().

Handler Function Signature

The signature of the handler is as follows −

void handler ( XMLParser $xml_parser, string|false $prefix, string $uri )

Parameters

Below are the parameters of the handler function −

  • $xml_parser: Specify a variable containing the XML parser calling the handler.

  • $prefix: It is the prefix string used to reference the namespace within an XML object. False if no prefix exists.

  • $uri: Uniform Resource Identifier (URI) of namespace.

Return Value

The xml_set_start_namespace_decl_handler() function returns TRUE on success. And FALSE on failure.

PHP Version

First introduced in core PHP 4.0.5, the xml_set_start_namespace_decl_handler() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP XML Parser xml_set_start_namespace_decl_handler() function. This program adds a simple handler to collect namespace definitions in an XML file.

<?php
   // Basic Namespace Handling Example
   function startNamespaceHandler($parser, $prefix, $uri) {
      echo "Namespace declared: Prefix = $prefix, URI = $uri\n";
   }

   // Create an XML parser
   $parser = xml_parser_create_ns();

   // Set the namespace handler
   xml_set_start_namespace_decl_handler($parser, 'startNamespaceHandler');

   // Parse a simple XML with a namespace
   $xml = '<root xmlns:ns="http://tutorialspoint.com/ns"><ns:child>Hello</ns:child></root>';
   xml_parse($parser, $xml);

   // Free the parser
   xml_parser_free($parser);
?>

Output

Here is the outcome of the following code −

Namespace declared: Prefix = ns, URI = http://tutorialspoint.com/ns

Example 2

In the below PHP code we will use the xml_set_start_namespace_decl_handler() function and show how to dynamically manage several namespaces by logging the URIs and namespace prefixes in an associative array.

<?php
   // Store namespace declarations
   $namespaceData = [];

   function startNamespaceHandler($parser, $prefix, $uri) {
      global $namespaceData;
      $namespaceData[$prefix] = $uri;
      echo "Namespace added: Prefix = $prefix, URI = $uri\n";
   }

   $parser = xml_parser_create_ns();
   xml_set_start_namespace_decl_handler($parser, 'startNamespaceHandler');

   $xml = '<root xmlns:x="http://tutorialspoint.com/x" xmlns:y="http://tutorialspoint.com/y">
               <x:child>Hello</x:child>
               <y:child>World</y:child>
         </root>';
   xml_parse($parser, $xml);
   xml_parser_free($parser);

   print_r($namespaceData); 
?> 

Output

This will generate the below output −

Namespace added: Prefix = x, URI = http://tutorialspoint.com/x
Namespace added: Prefix = y, URI = http://tutorialspoint.com/y
Array
(
   [x] => http://tutorialspoint.com/x
   [y] => http://tutorialspoint.com/y
)

Example 3

Now the below code uses the xml_set_start_namespace_decl_handler() function and checks namespaces against a preset list of valid URIs to detect incorrect namespaces at the time of processing.

<?php
   // Allowed namespace URIs
   $allowedNamespaces = ["http://tutorialspoint.com/ns1", "http://tutorialspoint.com/ns2"];

   function startNamespaceHandler($parser, $prefix, $uri) {
      global $allowedNamespaces;
      if (!in_array($uri, $allowedNamespaces)) {
         echo "Invalid namespace: URI = $uri\n";
      } else {
         echo "Valid namespace: Prefix = $prefix, URI = $uri\n";
      }
   }

   $parser = xml_parser_create_ns();
   xml_set_start_namespace_decl_handler($parser, 'startNamespaceHandler');

   $xml = '<root xmlns:valid="http://tutorialspoint.com/ns1" xmlns:invalid="http://unknown.com/ns">
               <valid:child>Hello</valid:child>
               <invalid:child>World</invalid:child>
         </root>';
   xml_parse($parser, $xml);
   xml_parser_free($parser);
?> 

Output

This will create the below output −

Valid namespace: Prefix = valid, URI = http://tutorialspoint.com/ns1
Invalid namespace: URI = http://unknown.com/ns
php_function_reference.htm
Advertisements