karl Jonson 0 Posted October 9, 2022 (edited) 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 October 9, 2022 by karl Jonson Making it clearer Share this post Link to post
Lars Fosdal 1792 Posted October 9, 2022 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
karl Jonson 0 Posted October 9, 2022 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
ertank 27 Posted October 9, 2022 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
karl Jonson 0 Posted October 9, 2022 (edited) 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 October 9, 2022 by karl Jonson Share this post Link to post
Lajos Juhász 293 Posted October 9, 2022 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. 1 Share this post Link to post
karl Jonson 0 Posted October 9, 2022 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
Stano 143 Posted October 9, 2022 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
David Heffernan 2345 Posted October 9, 2022 (edited) 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 October 9, 2022 by David Heffernan Share this post Link to post
Pat Foley 51 Posted October 9, 2022 (edited) 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 October 10, 2022 by Pat Foley add amended content Share this post Link to post
Lars Fosdal 1792 Posted October 10, 2022 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
Fr0sT.Brutal 900 Posted October 10, 2022 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. 1 Share this post Link to post
ZDeveloper 2 Posted October 13, 2022 you can easy do it with validation by XSD scheme Share this post Link to post