Strip namespaces from namespaced XML plugin response, so we get usuable output

This commit is contained in:
Jamie Shiell 2026-02-08 16:39:03 +00:00 committed by Benjamin Nussbaum
parent 0aa38428f6
commit afc29e15d5
2 changed files with 50 additions and 2 deletions

View file

@ -18,7 +18,7 @@ class XmlResponseParser implements ResponseParser
}
try {
$xml = simplexml_load_string($response->body());
$xml = $this->simplexml_load_string_strip_namespaces($response->body());
if ($xml === false) {
throw new Exception('Invalid XML content');
}
@ -43,4 +43,25 @@ class XmlResponseParser implements ResponseParser
return $array;
}
function simplexml_load_string_strip_namespaces($xml_response) {
$xml = simplexml_load_string($xml_response);
if ($xml === false) {
return false;
}
$namespaces = array_keys($xml->getDocNamespaces(true));
$namespaces = array_filter($namespaces, function($name) { return !empty($name); });
if (count($namespaces) == 0) {
return $xml;
}
$namespaces = array_map(function($ns) { return "$ns:"; }, $namespaces);
$xml_no_namespaces = str_replace(
array_merge(["xmlns="], $namespaces),
array_merge(["ns="], array_fill(0, count($namespaces), '')),
$xml_response
);
return simplexml_load_string($xml_no_namespaces);
}
}