XML with the correct syntax is called "well-formed" XML. XML that is validated by a DTD is "legitimate" XML. The role of a DTD (document type definition) is to define a legitimate building block for an XML document. It uses a series of legitimate elements to define the document structure. XML Schema is an XML-based DTD replacement. The XML Schema describes the structure of the XML document. The XML Schema language is also known as the XML Schema definition (XML Schema definition,xsd).
PowerDesigner is a case toolset for Sybase and can also be used to design XML models. Here I use PowerDesigner to design the XSD, using version 15.3.0.3248. Menu "File" → "New Model" → "Model Types" → "xml model" → "XML model Diagram", enter the model name under "model name", "XML Language" select "XML Schema Definition 1.0 ", as shown in:
Here for a simple test, just make a very simple model, if we want to test the content of an XML element is a prescribed integer, that is, the XML to be tested is as follows:
1 2 |
|
<?xml version= "1.0" encoding= "Utf-8"?> <number>abcd</number> |
Create a new Test.xml file, paste the above content, and save the file as "UTF-8" encoding. The purpose of the test is to detect that the <number> element content is not a defined integral type. In PowerDesigner, drag the Element icon on the Palette panel to the model diagram, double-click to set its "name" to "number", "type" to "Xs:integer" as shown in:
The right "preview" page allows you to preview the XSD text content. In the menu "Language" → "Generate XML Schema Definition file", select the saved path "Directory", press "OK" to generate the XSD file, open the generated "xmlmodel_test.xsd" file, The following content:
1 2 3 4 5 6 |
|
<?xml version= "1.0" encoding= "UTF-8"?> <xs:schema elementformdefault= "Qualified" xmlns:xs= "Http://www.w3.org/2001/XMLSchema" > <xs:element name= "number" type= "Xs:integer"/> </xs:schema> |
In Delphi 7 to verify the legitimacy of XML, you need to refer to the "msxml2_tlb" unit, this is through the menu "Project" → "Import Type Library" → "Microsoft xml,v6.0 (Version 6.0)", That is, "C:\WINDOWS\system32\msxml6.dll" and then clicking "Create Unit" generates "Msxml2_tlb.pas", as shown in:
Verify the sample code as follows:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 |
|
Uses msxml2_tlb;
Procedure Tform1.btn_valclick (Sender:tobject); Var Schemadoc, Xmldoc:ixmldomdocument2; Schemacache:ixmldomschemacollection; Error:ixmldomparseerror; Begin ---------------Load Data------------------------ XmlDoc: = codomdocument60.create; Xmldoc.async: = False; Xmldoc.load (' e:\ exercise \test.xml ');
---------------Load Schema Schema document------------------------ Schemadoc: = codomdocument60.create; Schemadoc.async: = False; Schemadoc.load (' e:\ exercise \test\xmlmodel_test.xsd ');
---------------registering the schema cache------------------------ SchemaCache: = coxmlschemacache60.create; Schemacache.add (', schemadoc);
---------------Allocation schema cache to an XML document------------------------ Xmldoc.schemas: = SchemaCache;
---------------Validation and reporting------------------------ Error: = xmldoc.validate; If Error.errorcode <> S_OK Then Mmo1. Lines.add (Error.reason) Else Mmo1. Lines.add (' verification success '); End |
Note that this is the use of "MSXML6" version, but also packaged into the source. Based on the above code, a GUI program looks like this:
: Http://dl.dbank.com/c02yqtgh32 http://download.csdn.net/detail/akof1314/3674176
http://blog.csdn.net/akof1314/article/details/6860264
Delphi 7 validating XML legitimacy (using DTDs, XSD)