DTD notes

DTD(Document Type Definition)Document type definition:

DTDUsed to define the structure of an XML document, as a content model for a canonical XML document, DTD is formalized in various domains
A unified specification document.

Use DTD in XML documents:

Internal DOCTYPE statement:

Wrap the DTD:&lt!! DOCTYPE root element in the XML document [element declaration]>

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE student [
  <!ELEMENT student (name,hometown,age)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT hometown (#PCDATA)>
  <!ELEMENT age (#PCDATA)>
]>
<student>
<name>Halen</name>
<hometown>unknown</hometown>
<age>ninteen</age>
</student>

 

External document declaration:

DTDAs a separate document:
QuoteShareDTDDocument: <!! DOCTYPE root element PUBLIC “file path and file name” >
QuoteprivateDTDDocument: <!! DOCTYPE root element SYSTEM “file path and file name” >

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE student SYSTEM "student.dtd">
<student>
<name>Halen</name>
<hometown>unknown</hometown>
<age>ninteen</age>
</student>
<!DOCTYPE student [
  <!ELEMENT student (name,hometown,age)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT hometown (#PCDATA)>
  <!ELEMENT age (#PCDATA)>
]>

Declare an element:

Leaf element declaration: <! ELEMENT element name category >
Element declaration: <!! ELEMENT element name (child element 1, child element 2…) >
Select child element declaration: <! ELEMENT element name (child element 1| sub element 2|…) >
 
The following values can be used for categories:
EMPTY     Empty element
#PCDATA   Character type data. When using #PCDATA in declaration, we need to enclose it with parentheses.
ANY       Arbitrary content
 
The number of control sub elements is:
Unsigned child elements appear only 1 times.
+       Child elements appear 1 or more times.
?       Child elements appear 0 or 1 times.
*       Child elements appear any time.

Declarative properties:

Rule of grammar:<!ATTLIST Element name property name property type default value >

Examples:
<!ATTLIST student name CDATA “unknown”>
<student name=”unknown”>
 
Attribute type options:
CDATA    The value is character data (character data).
(en1|en2|..)  This value is a value in the enumeration list.
ID     The value is the only ID.
IDREF    The value is ID of another element.
IDREFS    A list of other ID values
NMTOKEN   A XML name with a valid value.
NMTOKENS   A list of XML names with valid values.
ENTITY    Value is an entity.
ENTITIES   Value is a list of entities.
NOTATION   This value is the name of the symbol.
xml:    The value is a predefined XML value.
 
The following values can also be used for default values:
#REQUIRED   Attribute values are required.
#IMPLIED  Attributes are not required.
#FIXED value Attribute values are fixed.
The default value in XML is defined if the attribute value is not defined.

DTDEntity:

Internal common entity:A specific data defined in DTD can be quoted in DTD or XML.
Rule of grammar:<!ENTITY Entity name “entity value” >
Examples:
<!ENTITY unknown “BeiJing”>
<hometown>&unknown;</hometown>

 
External common entity:Entity objects defined outside document entities (DTD, XML documents)
Rule of grammar:<!ENTITY The entity name SYSTEM “URI/URL” >
Examples:
<!ENTITY unknown SYSTEM “hometown.txt>
<hometown>&unknown;</hometown>
&unknown;Representing the contents of the hometown.txt file
 
Internal parameter entity:Can only appear in DTD documents.
Rule of grammar:<!ENTITY % Entity name entity content >
Examples:
<!ENTITY % info “(name,age)”>
<!ELEMENT student %info;>
 
External parameter entity:Declared parameter entities in external DTD documents
Rule of grammar:<!ENTITY % The entity name SYSTEM “URI/URL” >

Leave a Reply

Your email address will not be published. Required fields are marked *