XML Technical DTD constraint definition

Source: Internet
Author: User
Tags file url xml parser

XML constraints

In XML technology, you can write a document that constrains the writing specification of an XML document, which is called an XML constraint
Why do I need XML constraints?

Class.xml <stu>< area; how can people have an AREA element?

XML file tags can be written casually, the DTD can be asked to write according to the reasonable demand

XML file Constraint--dtd

  Document type definition doc types definitions
Role:

Define XML tag constraints so that developers can write by definition or constrain HTML files

Use:
In a project where an XML file is required, the project manager can define a DTD file that requires all programmers to write XML files in accordance with this specification

Master requirements:
The corresponding XML file can be written according to the DTD file provided by others.

remark: DTD files must also be consistent with Utf-8 encoding and files
Introducing DTDs
Format:

  <! DOCTYPE root element name system "DTD path"> Local use System
Note: By default, the browser does not detect processing DTD constraints, it needs to write code tests or tools

File 1
checkdtd.html
This only for IE5 support other browsers do not support
<title> Test dtd</title>
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<script language= "JavaScript";
//Create an XML parser
var xmldoc=new ActiveXObject (" Microsoft.XMLDOM ");
//Turn on check function
xmldoc.validateonparse=true;
//Specifies the validation of the XML file
xmldoc.load ("Class.xml");
//If error, output error message
document.write ("error message =" +xmldoc.parseerror.reason+ "<br/ > ");
document.write ("error line number =" +xmldoc.parseerror.line);
</script>

Class.xml file
<?xml version= "1.0" encoding= "Utf-8"?>
<!--must ensure that the file is saved in the encoding method utf-8-->
<! DOCTYPE class SYSTEM "Class.dtd" >
<class>
<student ><!--Excellent student--
<name> Dujie </name>
<sex>boy</sex>
<age>28</age>
<MT>12312</MT>
</student>
</class>

XML.DTD file
<! ELEMENT class (student+) >
<! Element student (Name,age,des) > Student tag can have a name, age, introduction three elements
<! ELEMENT name (#PCDATA) > Description of the name
<! ELEMENT Age (#PCDATA) >
<! ELEMENT des (#PCDATA) >

DTD Document Declaration and reference equivalent to CSS
--Internal DTD document
Span style= "color: #ff0000;" ><! DOCTYPE root element [definition content]>
-- External DTD document
<! DOCTYPE root element SYSTEM "DTD file path" >
--internal and external DTD document combined
<! DOCTYPE root element system "DTD file path" [define content]>

The

reference DTD constraint
XML file applies the DOCTYPE declaration statement to indicate the DTD file it follows, and the DOCTYPE declaration statement has two forms
@ reference file is local
<! DOCTYPE Document root node system "DTD file path";
@ A common file when referencing a file
<! DOCTYPE Document root node  public "DTD name" "DTD file URL";
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD";

DTD file definition
<! ELEMENT NAME content>
<! ELEMENT des (#PCDATA) >
element keyword
Name Element name
Content element type (uppercase)
Content Type Analysis:
Empty the element cannot contain child elements and text, but can have attributes (empty elements)
Any this element can contain any element content defined in the DTD
#PCDATA can contain any character data, but cannot enclose it in a child tag ().

Combination type
<! ELEMENT Family (person +, home appliance *) >
  + elements one to many
  * elements 0 to multiple
  ? elements 0 to 1 times
  | Element selects a man|women in the listed objects at least one
  () element grouping
  , the object must appear in the order specified (Name,age,sex) Name,age,sex must appear and appear in the defined order


<! ELEMENT Family (person +, home appliance) >
If you remove * then the meaning is defined must have a home appliance

Property Definition
If the DTD file does not have a property defined, an error will be added to the XML file.
<! attlist element name
Attribute name Type attribute characteristics
Property name Type property features ...
>
Type analysis:

CDATA attribute values can be any character (including numbers and Chinese)
ID the value of this property must be unique (for labels of the same type) and start with a letter
Idref/idrefs ID Reference
The IDREF property value points to the value of the ID type declared elsewhere in the document
IDREFS with Irref but can have multiple references separated by spaces
Enumerated enumeration values in several values gender

Attribute characteristics
#REQUIRED This property must have a
#IMPLIED This property is optional
#FIXED value must be a fixed value for this property
Defalut value does not give the default value

How to : specify attributes for an element in the nearest principle
<! ELEMENT class (student+) >
<! Attlist Student
Address CDATA #REQUIRED
>

<! attlist person Sex (boy|girl) #REQUIRED >


Entity/entities Entity Definition

  Used to create an alias for a piece of content that can be referenced later in an XML document using an alias
In the DTD definition, a <! The Entity ...> statement is used to define a body
Entities can be divided into two types
  referencing Entities and parameter entities
Referencing entities
1. Defining in the DTD
  <! Entity name Content >
<! ENTITY sto "Personal Introduction" >
2. In XML References & entity names
 &sto; Reference symbol & and end semicolon; must have

Parameter entity
1. Defining a parameter entity in a DTD
  <! Entities% entity name "entity content">

2. Use it yourself in a DTD file
% entity name ;

DTD case:
TV Listings DTD
<! DOCTYPE Tvschedule [

<! ELEMENT tvschedule (channel+) >
<! ELEMENT CHANNEL (banner,day+) >
<! ELEMENT BANNER (#PCDATA) >
<! ELEMENT Day (DATE, (holiday| programslot+) +) >
<! ELEMENT HOLIDAY (#PCDATA) >
<! ELEMENT DATE (#PCDATA) >
<! ELEMENT Programslot (time,title,description?) >
<! ELEMENT time (#PCDATA) >
<! ELEMENT TITLE (#PCDATA) >
<! ELEMENT DESCRIPTION (#PCDATA) >

<! Attlist tvschedule NAME CDATA #REQUIRED >
<! Attlist CHANNEL CHAN CDATA #REQUIRED >
<! Attlist programslot VTR CDATA #IMPLIED >
<! Attlist TITLE RATING CDATA #IMPLIED >
<! Attlist TITLE LANGUAGE CDATA #IMPLIED >

]>
Write the corresponding XML document according to the DTD

XML Technical DTD constraint definition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.