XML DTD Validation

Source: Internet
Author: User
Tags xml example

XML DTD Validation

First, what is a DTD  

Document type definitions (dtd:document type definition) define legitimate XML document building blocks. It uses a series of legitimate elements to define the structure of the document.
DTDs can be declared in an XML document in rows or as an external reference.

  

Ii. role of the DTD

1. With a DTD, each XML can carry a description of its own format.

2. With DTDs, different organizations can use a common DTD to exchange data.

3. Whether or not XML data accepted from outside the application is valid using a DTD test.

  

Iii. what is included in the DTD

  1. Define rules for elements: in a DTD, you need to verify which elements in the XML can be included.

  2, the relationship between elements rules: refers to the element of the parent element is who, the element and elements in the end is the relationship between the upper and lower layers or parallel relations.

  3, the definition of the attribute: You can specify what the attribute name is in the XML, and what type the property value is.

Iv. Classification of DTDs:

  1. Internal DTD Documentation: The DTD document and the XML document are in the same file.

<! DOCTYPE root element [definition content]>

  2. External DTD Documentation: DTD documents and XML documents are saved in different files.

<! DOCTYPE root element SYSTEM "DTD file path" >

  3, internal and external combination of DTD documents: two kinds of combination of the wording.

<! DOCTYPE root element SYSTEM "DTD file path" [Definition content]>

V. Definition of DTD elements

Grammar:

<! ELEMENT NAME content>

ELEMENT: keyword that declares a DTD document.

Name: The element name, the names of the elements in the XML.

CONTENT: Element type,

Element types are: EMPTY, #PCDATA, pure element type, any.

  1. Empty:

The element cannot contain child elements and text, but can have attributes (empty elements).

Grammar:

  <! Element name Empty>

Example: Declaring an empty student element

<! ELEMENT Student Empty>

XML Example:

<student/>

  

  2. #PCDATA:

Can contain any character data, but cannot contain other child elements. Only PCDATA elements are declared by the #PCDATA in parentheses:

Grammar:

  <! Element name (#PCDATA) >

Example: declaring an element of name

<! ELEMENT name (#PCDATA) >

XML Example:

<name> Zhang San </name>

  3. Pure element type:

Can contain only child elements and cannot contain text. The order in which elements appear must be consistent with the definition.

Grammar:

<! element name (child element name 1, child element name 2,.....) >

Example: declaring student marks

<! ELEMENT student (Name,age) >

XML Example:

<student>

<name> Zhang San </name>

<age>18</age>

</student>


Use of symbols:
Symbol Use Example Description
() Used to group elements (Gu Yong | liang Yusheng), (Wang Shuo | yu jie), Mao Divided into three groups
| In the listed objects, you must select a (Men | women) Indicates that a man or woman must appear, at least one of them.
, Objects must appear in the order specified (Watermelon, apple, banana) Watermelon, apples, bananas must appear, and appear in the specified order.
* The object can occur 0 or more times (Hobby *) Hobbies can appear 0 times to many times
? The object can only appear 0 to 1 times (rookie?) Says rookie can appear 0 times to 1 times
+ objects can appear 1 to many times. (member +) Indicates that the member must appear 1 times to several times.


  4. Any:

Can contain any element type.

Grammar:

<! Element name Any>
Example:
<! ELEMENT Student Any>

Cases:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Studentlist [<!--declares that the root element can contain multiple student elements--><!ELEMENT studentlist (student*)><!--Declaration student can contain name,age,sex elements, name must appear 0-1 times, age appears 0-multiple times, sex must appear 1 times-<!ELEMENT student (name?, age*, Sex)><!--The declaration name can contain only text and cannot contain other tags-<!ELEMENT name (#PCDATA)><!ELEMENT Age (#PCDATA)><!--Declaring sex as an empty element-<!ELEMENT Sex EMPTY>]><Studentlist><Student><Name> Zhang San</Name><Age>20</Age><sex/> </student> <> <name< Span style= "color: #0000ff;" >> John Doe name> <sex/> </student> </studentlist>      

Vi. definition of attributes in a DTD

Grammar:

<! Attlist element Name Property Name property type default Value >

Description

Attlist: Attribute list keyword.

Element name: The name of the element to be decorated.

Property name: The name of the element who can.

Attribute type: CDATA, ID, IDREF/IDREFS, enumerated.

Property Features: #REQUIRED, #IMPLIED, #FIXED value, Default value.

  1) #REQUIRED: indicates that the element attribute must have a value for the property and cannot be empty.

  2) #IMPLIED: indicates that the element attribute can be ignored and can be empty.

  3) #FIXED value: indicates that the element attribute must be a specified fixed value, fixed value specified in value.

  4) default value: Indicates to the specified element defaults, does not need to use default, directly specify the defaults.

  Property Type:

  1. CDATA Properties

Indicates that the property value can be any character, including numbers and Chinese.

Example: Declaring a student property with a name value of any text that must have a value

DTD Example:

<! ATTLIST student name CDATA #REQUIRED >

XML Example:

<student name= "Zhangsan"/>

  2. ID attribute

Indicates that the property value must be unique

Example 1: Set the StudentID property to a unique value. and must have a value.

DTD Example:

<! Attlist Student ID #REQUIRED >

XML Example:

<student id= "10086"/>

Example 2: Set properties on multiple properties at the same time, ID for student

DTD Example:

<! ELEMENT student (Id,name) >

<! Attlist Student

ID ID #REQUIRED

Name CDATA #REQUIRED >

XML Example:

<student id= "10086" name= "Zhangsan" ></student>

  3, Idref/idrefs

IDREF: Indicates that the value of this property points to the value of the ID type declared elsewhere.

IDREFS: Same as IDREF, but multiple references can be separated by spaces.

Example:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE categories [<! ELEMENT categories (category*)><!ELEMENT category (category*)><!ATTLIST Category ID ID #REQUIRED name CDATA #REQUIRED parentid IDREF #IMPLIED><!--The ID of the ParentID reference outer layer in the child class-]><Categories><CategoryId= "C_1"Name= "Electrical"><CategoryId= "C_10"ParentID= "C_1"Name= "TV"/><CategoryId= "C_11"ParentID= "C_1"Name= "Refrigerator"/><CategoryId= "C_12"ParentID= "C_1"Name= "Refrigerator"/></Category><CategoryId= "C_2"Name= "mobile"><category id= "c_20" Span style= "color: #ff0000;" > Parentid= "c_2"  Name= "Smart phone" /> <id= "c_21"  Parentid= "c_2"  Name= " Non-smartphone "/> </></ Categories>          

  4, enumerated

Enumeration, which is predefined for some values, the value of the property must be in the list of worthy range classes.

DTD example: Student sex can only be male or female

<! attlist student Sex (male | female) #REQUIRED >

XML Example:

<student sex= "Male" >

VII. Entities

Constants defined in the DTD, used in XML. Used to define values that occur more than once in the XML.

Entities can be divided into internal entities and external entities:

  1. Internal entities

Grammar:

<! Entity entity name "Solid value" >

Example: Defining

DTD Example:

<! ENTITY Writer "Bill Gates" >
<! ENTITY Copyright "Copyright W3School.com.cn" >

XML Example:

<author>&writer;&copyright;</author>

Note: An entity consists of three components: a number (&), an entity name, and a semicolon (;).

  2. External Entities

Grammar:

<! Entity entities name SYSTEM "Uri/url" >

DTD Example:
<! ENTITY writer SYSTEM "Http://www.w3school.com.cn/dtd/entities.dtd" >
<! ENTITY copyright SYSTEM "HTTP://WWW.W3SCHOOL.COM.CN/DTD/ENTITIES.DTD" >
XML Example:
<author>&writer;&copyright;</author>

XML DTD Validation

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.