Xml
Fourth Chapter XML syntax
Seven. Syntax for DTDs
A DTD is a necessary file for a "valid XML document" that defines the rules and interrelationships of elements and identities in a document through a DTD file. How do I create a DTD file? Let's study together:
1. Set element
Elements are an essential part of an XML document. You define an element in the DTD and use it in an XML document. The definition syntax for an element is: <! ELEMENT DESCRIPTION (#PCDATA, DEFINITION) *>
Description
"<! Element "is the declaration of elements, stating that you want to define an element;
The "DESCRIPTION" after the declaration is the name of the element;
"(#PCDATA, DEFINITION) *>" is the usage rule for that element. Rules define what elements can contain and how they relate to each other. The following table outlines the rules for the elements:
2. Element Rule table: -->
Symbol |
Meaning |
Example |
#PCDATA |
Contains character or text data |
<myfile (#PCDATA) > Element myfile contains a text data |
#PCDATA, Element-name. |
Include text and other child elements |
<myfile (#PCDTATA, TITLE) > MyFile element must contain text and title child elements |
, |
Separating sorting with commas |
<myfile (Title,author,email) > The myfile element must, in turn, contain titile,author,email three child elements |
| |
Use the "|" Indicate or |
<myfile (TITLE | AUTHOR | EMAIL) > The myfile element must contain title, or author or email child elements. |
Name |
Can only be used once |
<myfile (TITLE) > The myfile element must contain the title child element and can only be used once. |
Name? |
Use one time or do not use |
<myfile (Title,author?,email?) > The myfile element must contain the title child element and can only be used once; it may contain or not include author and email child elements, but only once if used. |
name+ |
Use at least one or more times |
<myfile (Title+,author?,email) > The myfile element must contain the title child element and be used at least once, followed by the author child element, or not, and the email child must be included, and can only be used once. |
name* |
Use once, multiple times, or do not use at all |
<myfile (title*) > The myfile element can contain one, multiple, or no title child elements |
( ) |
Set up groups, you can nest |
<myfile (#PCDATA | TITLE) *> The element myfile contains one or more text or title child elements. <myfile ((title*, AUTHOR, EMAIL) * | COMMENT) > MyFile elements must contain content, content, or a comment, or multiple groups that contain: one, multiple, or no title child element, followed by one or no author, followed by a required email child element. |
In addition, we can also define attributes for the elements, because we do not recommend using attributes, and here we do not expand them in detail.
Finally, let's summarize some of the first four chapters and write a simple example that contains Dtd,xml and script to help readers understand:
1. Save the following file as Myfile.dtd
<! ELEMENT MyFile (title, author) >
<! ELEMENT title (#PCDATA) >
<! ELEMENT author (#PCDATA) >
2. Then create the XML document Myfile.xml:
<?xml version= "1.0" encoding= "GB2312"?>
<! DOCTYPE myfile SYSTEM "MYFILE.DTD" >
<myfile>
<title>xml Easy Learning Handbook </title>
<author>ajie</author>
</myfile>
3. Create HTML Document myfile.html
<script language= "JavaScript" for= "window" event= "onload" >
var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.async= "false";
Xmldoc.load ("Myfile.xml");
nodes = XmlDoc.documentElement.childNodes;
Title.innertext = Nodes.item (0). text;
Author.innertext = Nodes.item (1). text;
</script>
<title> call XML data in HTML </title>
<body bgcolor= "#FFFFFF" >
<b> Title: </b>
<span id= "title" ></span><br>
<b> Author: </b>
<span id= "Author" ></span><br>
</body>
4. Use IE5.0 above browser to open myfile.html can see the effect.
We already have some basic knowledge of XML. In the fifth chapter, I will explain a successful example of XML practical application, and show the powerful function of XML. Let's take a look at the next chapter: XML instance analysis.