The information in this article applies to:
Product: MIM and PM4Data
Version: 8.x
Platforms: Windows
Issue
- It is a common scenario to need to validate XML against an XSD using XMScript. How is this done?
Resolution
The following example applies to Windows and uses the XMScript named books.xms. This loads an XML document from books.xml and validates against books.xsd.
books.xms
// books.xms main() \{ var rc = CoInitialize(); var xsddoc = createObject("Msxml2.DOMDocument.4.0"); xsddoc.async = 0; var rc = xsddoc.load("books.xsd"); var xmldoc = createObject("Msxml2.DOMDocument.4.0"); var SchemaCache = createObject("Msxml2.XMLSchemaCache.4.0"); SchemaCache.add("",xsddoc); xmldoc.async = 0; xmldoc.validateOnParse = 0; xmldoc.schemas = SchemaCache; xmldoc.load("books.xml"); var pError = xmldoc.validate(); if (pError.errorCode.asShort() != short(0)) cout << "Houston, we have a problem: " << pError.errorCode.asShort() << " " << pError.reason << endl; else cout << "no problems!" << endl; if (xmldoc.parseError.errorCode != 0) \{ var myErr = xmldoc.parseError; cout << "You have error " << myErr.reason << endl; \} else \{ cout << xmldoc.xml << endl; \} \}
books.xml
// books.xml //the dollar sign has been added to the price 'Gambardella, Matthew' XML Developer's Guide Computer $44.95 2000-10-01 An in-depth look at creating applications with XML.
books.xsd
// books.xsd
Example output with the validation error:
C:\\Development\\XMScript>booksvalidate.xms PM XMScript Vd8.1.8.5 Copyright (c) 1997 - 2006 Metastorm, Inc., All Rights Reserved. Houston, we have a problem: -7804 Error parsing '$44.95' as float datatype. The element: 'price' has an invalid value according to its data type. 'Gambardella, Matthew' XML Developer's Guide Computer $44.95 2000-10-01 An in-depth look at creating applications with XML. VM terminated with rc = 0
Example output with the correction:
C:\\Development\\XMScript>booksvalidate.xms PM XMScript Vd8.1.8.5 Copyright (c) 1997 - 2006 Metastorm, Inc., All Rights Reserved. no problems! 'Gambardella, Matthew' XML Developer's Guide Computer 44.95 2000-10-01 An in-depth look at creating applications with XML. VM terminated with rc = 0