XML - The XML file DTD constraints Detailed

1.XML file a brief with the constraints of the DTD

We write the document to restrict write an XML document specification, which is called XML constraints.

Common technical constraints are:

  • XML DTD
  • XML Schema

DTD basic concepts:

document type definition document type definition

DTD files and XML files are generally used in conjunction with the main constraints to an XML file.

The introduction of XML file DTD file, so you can customize the XML tag, but subject to DTD files. For example, the use of an XML description of a class, if we give every student the definition of a <面积>label syntax is not wrong, but do not meet the semantics, the students how the area can be used to describe it? This time we need to use the DTD to constrain the XML.

<?xml version="1.0" encoding="gb2312"?>
<class>
    <stu id="001">
        <name>杨过</name> 
        <sex></sex>
        <age>20</age>
        <面积>100</面积>
    </stu>
</class>
  
  

1.1 DTD constraints Quick Start Case

The basic syntax:

<!ELEMENT 元素名 类型>
  
  

We are also in a class, for example, write the following DTD file, myClass.dtd:

<!ELEMENT 班级 (学生+)>
<!ELEMENT 学生 (名字,年龄,介绍)>
<!ELEMENT 名字 (#PCDATA)>
<!ELEMENT 年龄 (#PCDATA)>
<!ELEMENT 介绍 (#PCDATA)>
  
  

The first line indicates the root element for the class, students and the sub-element, or a plurality of sub-elements.
The second line indicates the student's child elements as the name, age, description
name below no children, then #PCDATA represents the name of which can put any text.
Age and presentation is similar.

MyClass.xml file and write the introduction DTD file as follows:

<?xml version="1.0" encoding="utf-8"?>
<!--引入dtd文件,约束这个xml-->
<!DOCTYPE 班级 SYSTEM "myClass.dtd">
<班级>
    <学生>
        <名字>周小星</名字>    
        <年龄>23</年龄>
        <介绍>学习刻苦</介绍>
    </学生>   
    <学生>
        <名字>林晓</名字> 
        <年龄>25</年龄>
        <介绍>是一个好学生</介绍>
    </学生>   
</班级>
  
  

Wrote in the introduction: SYSTEM, indicates that the current DTD file is local
if writing is PUBLIC, said the introduction of the DTD file from the network.

DTD file at this time is the introduction of no effect, if we add child elements in the student element <面积>, open the XML file, the browser still does not complain.

<?xml version="1.0" encoding="utf-8"?>
<!--引入dtd文件,约束这个xml-->
<!DOCTYPE 班级 SYSTEM "myClass.dtd">
<班级>
    <学生>
        <名字>周小星</名字>    
        <年龄>23</年龄>
        <介绍>学习刻苦</介绍>
        <面积>100平米</面积>
    </学生>   
    <学生>
        <名字>林晓</名字> 
        <年龄>25</年龄>
        <介绍>是一个好学生</介绍>
    </学生>   
</班级>
  
  

We need programming verify the correctness of XML documents .

More than IE5 browser built XML parsing tools: Microsoft.XMLDOM, developers can write JavaScript code, the use of this analytical tool to load XML files, and XML files DTD validation.

We write myXmlTools.html to verify this XML, as follows:

<html>
    <head>
    <!--自己编写一个简单的解析工具,去解析XML DTD是否配套-->     
    <script language="javascript">
        // 创建xml文档解析器对象
        var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        // 开启xml校验
        xmldoc.validateOnParse = "true";
        // 装载xml文档,即指定校验哪个XML文件
        xmldoc.load("myClass.xml");
        document.writeln("错误信息:"+xmldoc.parseError.reason+"<br>");
        document.writeln("错误行号:"+xmldoc.parseError.line);
    </script>

    </head>
    <body>

    </body>
</html>
  
  

Open the html file in IE browser, you can see the results:

XML file checksum

Line 9 can see what we've added <面积>this line.

2.DTD details

2.1 DTD declaration and reference document

1. Internal DTD document

<!DOCTYPE 根元素 [定义内容]>
  
  

2. External DTD document

The introduction of an external DTD document is divided into two types:

(1) When the DTD file is referenced when local files with SYSTEM logo, and write "the DTD file path", as follows:

<!DOCTYPE 根元素 SYSTEM "DTD文件路径">
  
  

(2) If the referenced DTD file is a common file, using PUBLIC identification, in the following manner:

<!DOCTYPE 根元素 PUBLIC "DTD名称" "DTD文件的URL">
  
  

Cases such as the following:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  
  

2.2 DTD basic syntax:

<!ELEMENT NAME CONTENT>
  
  

In which:
- the ELEMENT keyword, you can not be changed
- NAME represents the element name
- CONTENT element type, must be capitalized! CONTENT content writing, there are three:

(1) EMPTY-- indicates that the element can not contain sub-elements and text, but can have attributes.
(2) ANY-- indicates that the element can contain any of the elements defined in the DTD content in
(3) # PCDATA-- may contain any character data, but can not contain any child elements in which

2.3 DTD type combination of elements:

DTD in this provision:

<!ELEMENT 家庭(人+,家电*)>
  
  

The DTD specifies the elements of the family can have from one to multiple "person" sub-elements, you can have zero to many "home appliances" This child elements. The meaning of the plus sign "+" and asterisk "*" is consistent with the regular expression meaning.

XML write:

<家庭>
    < 名字="张晓明" 性别="男" 年龄="25"/>
    < 名字="李小钢" 性别="男" 年龄="36" 爱好="作个教育家和伟人"/>
    <家电 名称="彩电" 数量="3"/>
</家庭>
  
  

On the type of composition, there can be used the following modifiers:

symbol use Examples Example shows
() It used to group elements (Gu | Jin Yong), (Wang Shuo | Yu Jie) Divided into two groups
| Select one of the objects listed in (Men | Women) He represents the man or woman must appear, at least one of the two election
+ The object must occur one or more times (+ Members) The member must be present, and it may appear more members
* The objects are allowed zero or more times (Hobbies *) Hobbies may occur twice to several times
? The object must appear 0 or 1 times (Rookie?) Rookie may occur or may not occur, if present, can only occur at most once
, Objects must appear in the specified order (Watermelon, apples, bananas) Represents watermelon, apples, bananas must appear, and appear in this order

Attribute definition 2.4

DTD definition attributes is such that:

<!ATTLIST 元素名称
    属性名称 类型 属性特点
    属性名称 类型 属性特点......  
>
  
  

Wherein the type attribute has the following five kinds:

(1) CDATA
(2) ID
(3) IDREF/IDREFS
(4) Enumerated
(5) ENTITY/ENTITIES

Characteristic properties are the following four:

(1) #REQUIRED, indicates that the property must be given, it is not being given to
(2) #IMPLIED, indicates that the property may not be given to
(3) #FIXED value, indicates that the attribute value must be given a fixed value of
(4 ) default value, it indicates that the property has no value if it is assigned a default value value

For example, we would like to add the address of the property on the students in this sub-elements, and this attribute is required, for example:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE 班级 SYSTEM "myClass.dtd">
<班级>
    <学生 地址="香港">
        <名字>周小星</名字>    
        <年龄>23</年龄>
        <介绍>学习刻苦</介绍>
    </学生>   
    <学生 地址="澳门">
        <名字>林晓</名字> 
        <年龄>25</年龄>
        <介绍>是一个好学生</介绍>
    </学生>   
</班级>       
  
  

This time the corresponding DTD file should be updated, otherwise it will error, as follows:

<!ELEMENT 班级 (学生+)>
<!ELEMENT 学生 (名字,年龄,介绍)>
<!ATTLIST 学生
    地址 CDATA #REQUIRED
>
<!ELEMENT 名字 (#PCDATA)>
<!ELEMENT 年龄 (#PCDATA)>
<!ELEMENT 介绍 (#PCDATA)>
  
  

2.4.1 for a detailed explanation of the attribute type

(1) attribute type -CDATA, an attribute value can be any characters (including numbers and Chinese)

<!ATTLIST 木偶
    姓名 CDATA #REQUIRED
>
  
  
<木偶 姓名="匹诺曹"/>
<木偶 姓名="PiNuocao"/>
<木偶 姓名="123"/>
  
  

(2) the property type -ID, show that the value of this property must be unique, but the value of the property can not be based on figures at the beginning!

<!ELEMENT 公司职员 ANY>
<!ATTLIST 公司职员
    编号 ID #REQUIRED
    姓名 CDATA #REQUIRED
>
  
  
<公司职员 编号="Z001" 姓名="张三"/>
<公司职员 编号="Z002" 姓名="李思"/>
  
  

(3) Properties Type -IDREF / IDREFS
- IDREF attribute value point value of the ID document type declaration elsewhere
- IDREFS with IDREF, but may have a space to separate the plurality of reference.

<!ELEMENT 家庭(人+)>
<!ELEMENTEMPTY>
<!ATTLISTrelID ID #REQUIRED
    paraentID IDREFS #IMPLIED
    name CDATA #REQUIRED
>
  
  
<家庭>
    < relID="P_1" name="爸爸"/>
    < relID="P_2" name="妈妈"/>
    < relID="P_3" parentID="P_1 P_2" name="儿子"/>
</家庭>
  
  

(4) Properties Type -Enumerated, better value defined in advance, the value must be in the range of attribute values ​​listed.

<!ATTLIST person
    婚姻状态 (single|married|divorced|widowed) #IMPLIED
>
<!ATTLIST person
    性别 (男|女) #REQUIRED
>
  
  

(5) Properties Type -ENTITY, entity

Entity definition:
- an entity used as a piece of content to create an alias, since you can use an alias in an XML document referenced in the content of this.
-! In the DTD definition, an ENTITY statement defines an entity.
- entities can be divided into two types: entity references and parameter entities. Reference entity is an XML document applications, and parameter entities DTD file itself is applied.

① reference entities:

  • The main reference entity in an XML document is applied
    syntax is as follows, references to define the content of the entity best placed at the end DTD file.
<!ENTITY 实体名称 "实体内容">
  
  

Reference: & entity name; semicolon at the end to bring this reference converted directly to solid content

For example as follows:

<!ENTITY copyright "I am a programmer">
....
&copyright;
  
  

② parameter entity:

Parameter entities use the DTD file itself
syntax is:

<!ENTITY % 实体名称 "实体内容">
  
  

Reference is:% entity name

For example:

<!ENTITY % TAG_NAME "姓名|EMAIL|电话|地址">

<!ELEMENT 个人信息 (%TAG_NAME;|生日)>
<!ELEMENT 客户信息 (%TAG_NAME;|公司名)>
  
  

3.DTD actual case

DTD learning objectives are:
(1) requires us to be able to understand DTD file,
(2) we can write the corresponding XML documents against a DTD given

Let's look at a case, the following DTD file from a DTD is to take the case W3School online tutorial in coming, look each line, we should be able to understand.

<!ENTITY AUTHOR "John Doe">
<!ENTITY COMPANY "JD Power Tools, Inc.">
<!ENTITY EMAIL "jd@jd-tools.com">

<!ELEMENT CATALOG (PRODUCT+)>

<!ELEMENT PRODUCT
(SPECIFICATIONS+,OPTIONS?,PRICE+,NOTES?)>
<!ATTLIST PRODUCT
NAME CDATA #IMPLIED
CATEGORY (HandTool|Table|Shop-Professional) "HandTool"
PARTNUM CDATA #IMPLIED
PLANT (Pittsburgh|Milwaukee|Chicago) "Chicago"
INVENTORY (InStock|Backordered|Discontinued) "InStock">

<!ELEMENT SPECIFICATIONS (#PCDATA)>
<!ATTLIST SPECIFICATIONS
WEIGHT CDATA #IMPLIED
POWER CDATA #IMPLIED>

<!ELEMENT OPTIONS (#PCDATA)>
<!ATTLIST OPTIONS
FINISH (Metal|Polished|Matte) "Matte" 
ADAPTER (Included|Optional|NotApplicable) "Included"
CASE (HardShell|Soft|NotApplicable) "HardShell">

<!ELEMENT PRICE (#PCDATA)>
<!ATTLIST PRICE
MSRP CDATA #IMPLIED
WHOLESALE CDATA #IMPLIED
STREET CDATA #IMPLIED
SHIPPING CDATA #IMPLIED>

<!ELEMENT NOTES (#PCDATA)>
  
  

Then we can write the most simple XML file according to the DTD:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE CATALOG SYSTEM "product.dtd">
<CATALOG>
    <PRODUCT NAME="康帅傅矿泉水" CATEGORY="Table" PARTNUM="12" PLANT="Chicago">
        <SPECIFICATIONS WEIGHT="20" POWER="18">这里是细节</SPECIFICATIONS>
        <PRICE>25</PRICE>
        <PRICE>28</PRICE>
    </PRODUCT>
</CATALOG>
  
  

We then use the XML Microsoft.XMLDOM check, you will find no errors. But be careful coding.

Reprinted from: https://blog.csdn.net/gavin_john/article/details/51532756
Published 152 original articles · won praise 63 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_41617034/article/details/105370158