Monday, September 24, 2007

PHP5 DomDocument schemaValidation problems

First i have to tell about a SOAP Connection to a webservice. I don't know if the developers ever thought about SOAP more than just a quick thought how can we transfer data, since from my end it looks as if they have absolute no idea what they were doing.

We have a Service that holds a bunch of personal data which we have to transfer to the webservice of an higher instance. Now you would think they provide a SOAP Connection where there are a lot of functions to send them the data for every person.. but no.. there are only two functions:
  • One which wants a XML String where all personal data has been collected together and
  • a second one which wants the same string as zip
So you see the whole point of the SOAP connection is just to send them the already prepared XML Files and then they work with the collected data somehow internal.

Fortunately they provided a xsd file so i was able to add additional validation to ensure the data is valid.

Now i created the XML Files using php5 DomDocument. The library makes it really easy to create Elements and it also holds a schemaValidation functionality.

I don't want to get into to much detail but when i said that they want all personal data of all people collected in one file you will notice that it would not be a good idea creating first all users in one file and then check if that file is ok... or perhaps i am just the only paranoid here, but i wanted to know exactly which person is making troubles.

So my code first creates the xml data for each person separately (although cached) and then i add the xml data to an prepared xml so i can validate against the schema.

The Problem
Now we get to the point. My Function first creates a Validation XML which helds all the markup and then adds the XML Data of the person using DomDocument->importNode and then appendChild at the correct position.

Unfortunately the schemaValidation finds errors where there are not really one, or so i thought. I got Error Messages which i was unable to see anything wrong. But i found a workaround.
Using DomDocument->loadXML(DomDocument->saveXML()) just before the validation i was able to sort of "normalize" the DomDocument and the schemaValidation worked.

I am not sure if the schemaValidation was just not designed for such a work or i did misjudge and used it completely wrong. But since there is not much documentation at php.net i had no other way.

No comments: