Introduction to DTD

A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes.

A DTD can be declared inline inside an XML document, or as an external reference.

Internal DTD Declaration

If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element [element-declarations]>

Example XML document with an internal DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don‘t forget me this weekend</body>
</note>

Open the XML file above in your browser (select "view source" or "view page source" to view the DTD)

The DTD above is interpreted like this:

  • !DOCTYPE note defines that the root element of this document is note
  • !ELEMENT note defines that the note element contains four elements: "to,from,heading,body"
  • !ELEMENT to defines the to element to be of type "#PCDATA"
  • !ELEMENT from defines the from element to be of type "#PCDATA"
  • !ELEMENT heading defines the heading element to be of type "#PCDATA"
  • !ELEMENT body defines the body element to be of type "#PCDATA"

External DTD Declaration

If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element SYSTEM "filename">

This is the same XML document as above, but with an external DTD (Open it, and select view source):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "C:\Users\maijunjin\Desktop\dtd\note.dtd">
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don‘t forget me this weekend!</body>
</note>

And this is the file "note.dtd" which contains the DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Why Use a DTD?

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

With a DTD, independent groups of people can agree to use a standard DTD for interchanging data.

Your application can use a standard DTD to verify that the data you receive from the outside world is valid.

You can also use a DTD to verify your own data.

时间: 2024-12-05 01:37:13

Introduction to DTD的相关文章

Spring AOP之Introduction(@DeclareParents)简介

Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarations in AspectJ) enable an aspect to declare that advised objects implement a given interface, and to provide an implementation of that interface on be

XML的DTD和Schema约束

为什么要使用约束? XML是自定义的标签,有时候标签太多,记不住,所以就需要有约束来告诉我能写哪些标签,哪些标签写错了不能识别 XML中有哪几种约束? 有很多约束,其中DTD和Schema约束最为常见. 约束本质上是什么? 约束本质上也是一种xml文件. DTD约束和Schema约束的区别 特点的区别: DTD约束较为古老,简单,一些老框架使用DTD作为约束:Struts2和Hibernate都使用DTD作为其XML配置文件的约束 Schema约束功能更为强大,用的更为广泛,Tomcat和Spr

Myeclipse中struts2配置文件配置dtd以支持自动补全

如果在eclipse中配置只需要,只需要在菜单栏window->preference->Myeclipse->file and eiditor->xml->xml catalog->add添加: Location中选中struts-2.3.dtd所在位置如下图所示 Key Type选中URI Key:在struts.xml中有   <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD St

【超实用】图解--如何使用本地的dtd文件映射

以前一直很苦恼,如果电脑上不了网,就比较麻烦了,自己在配置HIbernate的属性的时候,不知道属性名有没有写错.. 现在和大家分享一下,毕竟自己痛苦过了,大家不要和我一样痛苦. [超实用]图解--如何使用本地的dtd文件映射,布布扣,bubuko.com

Introduction and Basic concepts

1 Network Edge The device such as computers and mobiles connect to the Internet. So they are referred as end systems(who run the application programs) sitting at the edge of the Internet. And we use host and end system interchangeably, that is host=e

DTD验证XML文档

DTD验证XML文档        1.DTD简介:DTD是Document Type Definition的缩写,即文档定义            1.1:DTD的内容包含:                    元素定义规则                    元素之间的关系规则                    属性的定义规则            1.2:DTD的作用如下:                    DTD使每个XML文件可以携带一个自身格式的描述          

Referenced file contains errors (http://tiles.apache.org/dtds/tiles-config_3_0.dtd)

java开发时遇到的问题,之前还是好好的,没有错误提示.可是今天一打开项目就出现这种问题.真不知道是怎么回事,在这里求助.错误如下: Referenced file contains errors (http://tiles.apache.org/dtds/tiles-config_3_0.dtd). For more information, right click on the message in the Problems View and select "Show Details...&

Introduction to Machine Learning

Chapter 1 Introduction 1.1 What Is Machine Learning? To solve a problem on a computer, we need an algorithm. An algorithm is a sequence of instructions that should be carried out to transform the input to output. For example, one can devise an algori

DTD与XML基本语法规则

DTD(文档类型定义)可以定义合法的XML文档结构,它使用一系列合法元素来定义文档的结构.DTD分为内部DTD和外部DTD,所谓内部DTD是指该DTD在某个文档的内部,只被该文档使用.外部DTD是指该DTD不在文档内部,可以被其他所有的文档来共享.DTD文档与XML文档实例的关系可以看成是类和对象的关系. (1)外部DTD文件的编写及引用 新建一个外部family.dtd文件 <!ELEMENT family (father,mother,son+)> <!ELEMENT father