PHP xml_parser_create_ns() Function

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

Definition

In PHP, xml_parser_create_ns() creates an XML parser that understands namespaces and returns it as an XMLParser object.

A namespaced tag is reported with its namespace URI first, then a separator, then the local name, for example urn:demo|item. The default separator is a colon, and you can pick another one when the URI itself contains colons.

Syntax

Call xml_parser_create_ns() with the values you need. Start with this line.

PHP
$parser = xml_parser_create_ns(null, '|');
CallWhat it does
xml_parser_create_ns()Creates a namespace-aware parser with the default colon separator.
xml_parser_create_ns(null, '|')Uses a pipe as the separator, which is easier to split when URIs contain colons.
xml_parser_create_ns('UTF-8', '|')Sets the source encoding and the separator.

Use it when your XML has xmlns declarations. Names in a namespace come to your handlers as URI, separator, and local name. Tags without a namespace keep their plain name.

Library declarations
PHP
xml_parser_create_ns(?string $encoding = null, string $separator = ":"): XMLParser

Parameters

ParameterDescription
encodingOptional. The source encoding: ISO-8859-1, UTF-8, or US-ASCII. The default is null.
separatorOptional. The string placed between the namespace URI and the local name. The default is a colon (:).

Return Value

Return ValueDescription
XMLParserA namespace-aware parser object for use with the other xml_* functions.

PHP Examples

Example 1 — Use a Custom Separator

Create a namespace-aware parser with a pipe separator and print the names given to the start handler.

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

$parser = xml_parser_create_ns(null, '|');
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

xml_set_element_handler(
    $parser,
    function ($parser, $name, $attrs) {
        echo "Start: ", $name, PHP_EOL;
    },
    function ($parser, $name) {
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. xml_parser_create_ns(null, '|') makes a namespace-aware parser that joins names with a pipe.
  2. Case folding is turned off so the URI keeps its original case.
  3. The xmlns on root gives both tags the urn:demo namespace.
  4. Each name arrives as the URI, the pipe, and the local name.

Example 2 — Default Separator and No Namespace

With the default colon separator the names look like urn:demo:root. A tag without a namespace is not changed.

PHP
<?php
function showNames(string $xml): void
{
    $parser = xml_parser_create_ns();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

    xml_set_element_handler(
        $parser,
        function ($parser, $name, $attrs) {
            echo "Start: ", $name, PHP_EOL;
        },
        function ($parser, $name) {
        }
    );

    xml_parse($parser, $xml, true);
}

showNames('<root xmlns="urn:demo"><item>Tea</item></root>');
showNames('<root><item>Tea</item></root>');

How It Works

  1. Calling xml_parser_create_ns() with no arguments uses the default encoding and the colon separator.
  2. The first document declares a namespace, so its names start with urn:demo and a colon.
  3. The colon inside the URI and the separator look alike, which is why a custom separator can be easier to split.
  4. The second document has no namespace, so the names stay plain.

Example 3 — Split the URI and Local Name

Pick a separator that never appears in the URI, then use explode() to separate the namespace from the local name.

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

$parser = xml_parser_create_ns(null, '|');
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

xml_set_element_handler(
    $parser,
    function ($parser, $name, $attrs) {
        [$uri, $local] = explode('|', $name, 2);
        echo "URI=", $uri, " LOCAL=", $local, PHP_EOL;
    },
    function ($parser, $name) {
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. The prefix a is only a nickname. The parser replaces it with the real URI urn:a.
  2. The handler receives urn:a|root and urn:a|item.
  3. explode('|', $name, 2) splits each name at the first pipe.
  4. The list assignment puts the two parts into $uri and $local.

Frequently Asked Questions

It creates an XMLParser object that understands XML namespaces. Namespaced tag names are passed to your handlers as the namespace URI, the separator, and the local name.
A colon (:). Because many URIs also contain colons, a character such as | is often easier to split.
No. They keep their plain name, so you can use the same handlers for both kinds.
Case folding is on by default and it changes the whole name. Turn it off with xml_parser_set_option() and XML_OPTION_CASE_FOLDING.
Both return an XMLParser object. The only difference is that xml_parser_create_ns() handles namespaces and accepts a separator.

Next: xml_parser_free()

xml_parser_free() is a legacy function that used to release an XML parser from memory.

xml_parser_free() 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