Jump to content
Sign in to follow this  
karl Jonson

XML validation

Recommended Posts

XML validation.
I have an Object Pascal application which generates XML similar to this one:

<book>
  <name>Anna Karenina</name> 
  <author>Leo Tolstoy</author> 
  <publisher>The Russian Messenger</publisher>
  <price>$16.79</price>
</book>


Which is the best way to add code which validates the above XML (in the generating app) ?

I would like to make sure that: 
A book must have an author.
A book price cannot exceed $100

TIA

Edited by karl Jonson
Making it clearer

Share this post


Link to post

Do you want to do it in the generating app, or do you want to check the XML by validating against a schema (xsd)?

Personally, I would have done it in Delphi. So much faster and a lot less trouble.

Share this post


Link to post
7 minutes ago, Lars Fosdal said:

Do you want to do it in the generating app, or do you want to check the XML by validating against a schema (xsd)?

Personally, I would have done it in Delphi. So much faster and a lot less trouble.

I would like to validate the XML in the generating app but not against an XSD schema.

So my app generates the XML & as a last step I would like to validate the XML against a list of rules, e.g.

A book must have an author.
A book price cannot exceed $100

A book publisher must exist in a pre-defined list.

A book ISBN must be unique

....

Then generate a report at the end of validation which lists all errors found.

Thanks.

 

Share this post


Link to post

Is there a reason for you not to check each XML item against these rules at the time of XML generation?

Share this post


Link to post
31 minutes ago, ertank said:

Is there a reason for you not to check each XML item against these rules at the time of XML generation?

There's no reason.  That's something I need to consider.  

 

These are the steps I had in mind:

User clicks on "Generate XML" button

App generates XML

App validates XML

A report containing errors is generated & displayed to user

User goes through errors report, correct their data, then click on "Generate XML" again.

Edited by karl Jonson

Share this post


Link to post

There is no reason to verify the data after you generated the XML. It's always the best to verify in case the data is incorrect to change it and as a last step to generate the output.

  • Like 1

Share this post


Link to post
2 minutes ago, Lajos Juhász said:

There is no reason to verify the data after you generated the XML. It's always the best to verify in case the data is incorrect to change it and as a last step to generate the output.

These are the steps I had in mind:

User clicks on "Generate XML" button

App generates XML

App validates XML

A report containing errors is generated & displayed to user

User goes through errors report, correct their data, then click on "Generate XML" again.

Share this post


Link to post

They all tell you the same thing: check the data and then generate the XML.
It's exactly the same procedure as with DB data, or anything else.

Share this post


Link to post

Very hard to understand why you'd want to give away all the knowledge of your domain that is in your delphi code and be determined to work on the xml for this.

 

But if you want to validate the xml because your mind is made up, then crack on. 

Edited by David Heffernan

Share this post


Link to post
22 hours ago, karl Jonson said:

A book ISBN must be unique

Fishing on www for the ISBN--if an ISBN is assigned extract Author and Title in XML--the third answer in SO question https://stackoverflow.com/questions/2454348/isbn-bookdata-lookup-to-fill-in-a-database.  

Fishing on www for the ISBN would allow auto-fill when not unique.

Edited by Pat Foley
add amended content

Share this post


Link to post

The GIGO rule applies.  Garbage in -> Garbage out. 
Hence, clean up your data by doing your validation during input.

Share this post


Link to post

XML validation would only have sense if you had configurable set of validation rules (f.i. as another XML or XSD). Otherwise I see no reason to validate output rather than source data.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×