miho
miho

Reputation: 12085

Tell XMLReader to ignore provided namespace info

I'm using an instance of PHPs built-in XMLReader to read some kind of user-generated XML file. Usually this XML files content starts like the following sample snippet, where everything works fine:

<?xml version="1.0" encoding="UTF-8"?>
<openimmo>
  <uebertragung art="OFFLINE" umfang="VOLL" version="1.2.7" (...)

However, another user uses a different software to send and generate the XML file. The XML generated by this software starts like:

<?xml version="1.0" encoding="UTF-8"?>
<openimmo xmlns="http://www.openimmo.de" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openimmo.de openimmo.xsd">
  <uebertragung art="OFFLINE" umfang="VOLL" version="1.2.7" (...)

Which causes my importer to fail with the following error:

XMLReader::read(): Element '{http://www.openimmo.de}openimmo': No matching global declaration available for the validation root.

I'm already doing validation by manually applying some XSD schema. The passed file follows the same schema, just explicitly specifies the xmlns attributes. How can I work around this issue? How can I tell XMLReader to just ignore that xmlns statement?

My code (simplified to the relevant sections) looks like the following snippet:

$reader = new XMLReader();
$success = @$reader->open($path);
if (!$success) { /* error handling */ }
$reader->setSchema($localOpenImmoXsdPath);
/* then starts reading and throws the above exception */

Upvotes: 1

Views: 337

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

Namespace information is fundamental and there's no way an XML parser is going to ignore it.

Your options are either (a) send the file back to sender, saying it doesn't conform to the agreed schema, or (b) transform the file sent to you so that it does conform, by changing the namespace. That's a fairly simple XSLT transformation.

My immediate instinct was to look at the OpenImmo specs to see what they say about namespaces and schema conformance, but unfortunately access to the specs requires registration and licensing. Basically, either the specs allow both these formats, which would be a pretty shoddy spec, or they only allow one of them, in which case you shouldn't be accepting both.

Upvotes: 1

Related Questions