XML constraint technology: DTD constraint: syntax is relatively simple and function is relatively simple. schema constraints: The syntax is relatively complex and functions are relatively powerful. (namespace)
DTD constrained import DTD mode:
Internal Import <! DOCTYPE Note [ <! ELEMENT Note (to,from,heading,body) > <! ELEMENT to (#PCDATA) > <! ELEMENT from (#PCDATA) > <! ELEMENT heading (#PCDATA) > <! ELEMENT body (#PCDATA) >]>
external import of local file system :<! DOCTYPE Note SYSTEM "Note.dtd" > External import of public:<! DOCTYPE root element Public "HTTP://GZ.ITCAST.CN/ITCAST.DTD" >
DTD syntax constraint tag <! Element name category > or <! Element elements name (element content) > Category: empty tag: null. Indicates that the element must be an empty element. Normal string: (#PCDATA). Indicates that the content of an element must be a normal string (cannot contain child tags).
(element content): Order problem:<! element name (child element name 1, child element name 2,.....) >: Number of sub-labels in order problem: Tags: must and only appear 1 times. Label+: Labels appear at least 1 times*: 0 or N times. Label? :0or 1 times. Constraint Properties<! Attlist element Name Property Name property type default Value >Default Value: #REQUIRED property value is required #IMPLIED property is not required #FIXED The Value property is not required, but the property value is a fixed property type: A CDATA that controls the value of a property: normal String (En1|en2|..) : Indicates that it must be one of the value IDs: Indicates that the attribute value must be unique in an XML document. Value cannot start with a number
Code:
Create a new Note.dtd file : <! ELEMENT Note (from?,to+,heading*,body+) > <! ELEMENT to empty> <! ELEMENT from (#PCDATA) > <! ELEMENT heading (#PCDATA) > <! ELEMENT body (#PCDATA) > <! Attlist to ID ID #REQUIRED >
Create a new note.xml<?xml version= "1.0"? ><! DOCTYPE Note SYSTEM "note.dtd" ><note> <to id= "A1" ></to> <to id= "A2" ></to> <to id= "A3" ></to>
The DTD FOR XML constraints