mandag den 9. januar 2012

Validating xml with a schema

A XML Schema is a contract on how the XML structure should look. In a data transmission both parts know how the XML document should/could look.

XML Schemas can only be used with XMLDocument60 previous versions only support DTD.

Implicit validation:
 doc.validateOnParse := true;  

Explicit validation:
 doc.validate();  

Complete CodeUnit examble:

 OBJECT Codeunit 90003 SchemaValidation  
 {  
  OBJECT-PROPERTIES  
  {  
   Date=09-01-12;  
   Time=15:22:39;  
   Modified=Yes;  
   Version List=;  
  }  
  PROPERTIES  
  {  
   OnRun=BEGIN  
       ImplicitValidate();  
       ExplicitValidate();  
      END;  
  }  
  CODE  
  {  
   VAR  
    XmlDocument@1160870000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A05-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.DOMDocument60";  
    XmlSchema@1160870001 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A05-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.DOMDocument60";  
   PROCEDURE ImplicitValidate@1160870001();  
   VAR  
    XmlSchemaCache@1160870000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A07-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.XMLSchemaCache60";  
   BEGIN  
    CLEAR(XmlDocument);  
    CLEAR(XmlSchemaCache);  
    CREATE(XmlSchemaCache);  
    CREATE(XmlDocument);  
    XmlDocument.async := FALSE;  
    // Populating Schema  
    LoadSchema();  
    // Apply schema to schemacache with namespace. '' apply to default namespace  
    XmlSchemaCache.add('', XmlSchema);  
    // apply schema to xmldocument  
    XmlDocument.schemas := XmlSchemaCache;  
    // validate data when loading of XML is finished  
    XmlDocument.validateOnParse := TRUE;  
    // loading xml into document. XML is parsed when loading is done and false is returned if schema validation failed  
    IF LoadXmlDocument() THEN BEGIN  
     MESSAGE('VALID IMPLICIT');  
    END  
    ELSE BEGIN  
     MESSAGE('ERROR IMPLICIT');  
    END;  
    // loading xml into document. XML is parsed when loading is done and false is returned if schema validation failed  
    IF LoadXmlDocumentWithError() THEN BEGIN  
     MESSAGE('VALID IMPLICIT');  
    END  
    ELSE BEGIN  
     MESSAGE('ERROR IMPLICIT');  
    END;  
   END;  
   PROCEDURE ExplicitValidate@1160870005();  
   VAR  
    XmlSchemaCache@1160870000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A07-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.XMLSchemaCache60";  
    XmlParseError@1160870001 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{3EFAA428-272F-11D2-836F-0000F87A7782}:'Microsoft XML, v6.0'.IXMLDOMParseError2";  
   BEGIN  
    CLEAR(XmlDocument);  
    CLEAR(XmlSchemaCache);  
    CREATE(XmlSchemaCache);  
    CREATE(XmlDocument);  
    XmlDocument.async := FALSE;  
    // Populating Schema  
    LoadSchema();  
    // Apply schema to schemacache with namespace. '' apply to default namespace  
    XmlSchemaCache.add('', XmlSchema);  
    // apply schema to xmldocument  
    XmlDocument.schemas := XmlSchemaCache;  
    // validate data when loading of XML is finished  
    XmlDocument.validateOnParse := FALSE;  
    // loading xml into document. XML is not parsed when loading is done and true is returned unless the XML convension is not complied  
    LoadXmlDocument();  
    // XML content is validated against the schema and a validation result object is returned.  
    XmlParseError := XmlDocument.validate();  
    // If errorCode is 0 then there was no errors in the validation  
    IF XmlParseError.errorCode = 0 THEN  
    BEGIN  
     MESSAGE('VALID EXPLICIT');  
    END  
    ELSE BEGIN  
     MESSAGE('ERROR EXPLICIT: ' + XmlParseError.reason + ' - ' + XmlParseError.srcText + ' - ' + FORMAT(XmlParseError.line));  
    END;  
    // loading xml into document. XML is not parsed when loading is done and true is returned unless the XML convension is not complied  
    LoadXmlDocumentWithError();  
    // XML content is validated against the schema and a validation result object is returned.  
    XmlParseError := XmlDocument.validate();  
    // If errorCode is 0 then there was no errors in the validation  
    IF XmlParseError.errorCode = 0 THEN  
    BEGIN  
     MESSAGE('VALID EXPLICIT');  
    END  
    ELSE BEGIN  
     MESSAGE('ERROR EXPLICIT: ' + XmlParseError.reason + ' - ' + XmlParseError.srcText + ' - ' + FORMAT(XmlParseError.line));  
    END;  
   END;  
   PROCEDURE LoadSchema@1160870002();  
   BEGIN  
    CLEAR(XmlSchema);  
    CREATE(XmlSchema);  
    XmlSchema.async := FALSE;  
    XmlSchema.loadXML(  
    '<?xml version="1.0" encoding="utf-8" ?>' +  
    '<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">' +  
     '<xs:element name="xmltest">' +  
      '<xs:complexType>' +  
       '<xs:sequence>' +  
        '<xs:element name="elem1">' +  
         '<xs:complexType>' +  
          '<xs:sequence>' +  
           '<xs:element name="elem2" type="xs:string" minOccurs="1" />' +  
           '<xs:element name="elem3" type="xs:unsignedByte" />' +  
           '<xs:element name="elem4" type="xs:decimal" />' +  
          '</xs:sequence>' +  
         '</xs:complexType>' +  
        '</xs:element>' +  
       '</xs:sequence>' +  
      '</xs:complexType>' +  
     '</xs:element>' +  
    '</xs:schema>');  
   END;  
   PROCEDURE LoadXmlDocument@1160870003() LoadSuccess : Boolean;  
   BEGIN  
    LoadSuccess := XmlDocument.loadXML(  
    '<?xml version="1.0" encoding="utf-8" ?>' +  
    '<xmltest>' +  
     '<elem1>' +  
      '<elem2>v‘rdi</elem2>' +  
      '<elem3>3</elem3>' +  
      '<elem4>1.5</elem4>' +  
     '</elem1>' +  
    '</xmltest>');  
   END;  
   PROCEDURE LoadXmlDocumentWithError@1160870004() LoadSuccess : Boolean;  
   BEGIN  
    LoadSuccess := XmlDocument.loadXML(  
    '<?xml version="1.0" encoding="utf-8" ?>' +  
    '<xmltest>' +  
     '<elem1>' +  
    //  '<elem2>v‘rdi</elem2>' + // This makes an error <xs:element name="elem2" type="xs:string" minOccurs="1" />  
      '<elem3>3</elem3>' +  
      '<elem4>1.5</elem4>' +  
     '</elem1>' +  
    '</xmltest>');  
   END;  
   BEGIN  
   {  
    Shows how to validate XML using a XML-Schema  
   }  
   END.  
  }  
 }  

Ingen kommentarer:

Send en kommentar