Reputation: 9050
I'm trying to build a tool for validating specific XML and outputting human-readable results. My existing PHP code uses DOMDocument::schemaValidate to validate the XML against a .xsd file, but the error messages aren't very friendly.
For example, Let's pretend my XML looks like the sample below and that Latitude should be a decimal:
<Person>
<FirstName>Bret</FirstName>
<Location>
<Address>123 Test Ave.</Address>
<Latitude></Latitude>
<Longitude>30.139338</Longitude>
</Location>
</Person>
The error message I would currently get would be:
Document::schemaValidate(): Element 'Latitude': '' is not a valid value of the atomic type 'xs:decimal'.
I want it to say:
Error in XML Line #5: The person "Bret" has an empty Latitude in their address. You must include a valid Latitude in order for us to process this XML.
Similar questions has been asked before:
However, I'm willing to write my own brute-force validation code using PHP or Perl if DOMDocument::schemaValidate isn't the right tool for this job. Notice that my error messages need to refer to information stored in parent elements, like the "FirstName" field in my example.
My question is: What's a good approach to create this validation tool? Are there any existing code libraries or tutorials to point me in the right direction?
Thanks!!
Upvotes: 0
Views: 827
Reputation: 4996
As you can imagine this is specific to each XML file and format.
The first problem I would start to work with is to identify the exact element that relates to the validation error. E.g. with your error that is
/Person/Location/Latitude
This would be a pre-condition to tell in an error message that something is wrong with a specific person (element).
The second problem is to keep record of the main building blocks of your structure.
E.g. it's known that Latitude is part of Location which is part of Person. This knowledge you need to manage somewhere to give better error messages.
Also I would do both, e.g. outputting a more friendly error message as well as the more technical one. Also take care that you can not always solve your problem. E.g. if in the input a Person is given the wrong tagname like person you can never resolve this to a human friendly error message, because this is out of the knowledge you can add.
Upvotes: 1