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

