我已经谷歌了这个问题,但是我不太清楚什么是XML模式和DTD(文档类型定义),以及为什么XML模式比DTD更强大。
非常感谢您的指导。
来自DTD和Schema之间的区别部分的将DTD转换为Schema文章:
The critical difference between DTDs and XML Schema is that XML Schema utilize an XML-based syntax, whereas DTDs have a unique syntax held over from SGML DTDs. Although DTDs are often criticized because of this need to learn a new syntax, the syntax itself is quite terse. The opposite is true for XML Schema, which are verbose, but also make use of tags and XML so that authors of XML should find the syntax of XML Schema less intimidating.
The goal of DTDs was to retain a level of compatibility with SGML for applications that might want to convert SGML DTDs into XML DTDs. However, in keeping with one of the goals of XML, "terseness in XML markup is of minimal importance," there is no real concern with keeping the syntax brief.
[...]
So what are some of the other differences which might be especially important when we are converting a DTD? Let's take a look.
Typing
The most significant difference between DTDs and XML Schema is the capability to create and use datatypes in Schema in conjunction with element and attribute declarations. In fact, it's such an important difference that one half of the XML Schema Recommendation is devoted to datatyping and XML Schema. We cover datatypes in detail in Part III of this book, "XML Schema Datatypes."
[...]
Occurrence Constraints
Another area where DTDs and Schema differ significantly is with occurrence constraints. If you recall from our previous examples in Chapter 2, "Schema Structure" (or your own work with DTDs), there are three symbols that you can use to limit the number of occurrences of an element: *, + and ?.
[...]
Enumerations
So, let's say we had a element, and we wanted to be able to define a size attribute for the shirt, which allowed users to choose a size: small, medium, or large. Our DTD would look like this:
<!ELEMENT item (shirt)> <!ELEMENT shirt (#PCDATA)> <!ATTLIST shirt size_value (small | medium | large)>
[...]
But what if we wanted
size
to be an element? We can't do that with a DTD. DTDs do not provide for enumerations in an element's text content. However, because of datatypes with Schema, when we declared the enumeration in the preceding example, we actually created asimpleType
calledsize_values
which we can now use with an element:
<xs:element name="size" type="size_value">
[...]
正如许多人先前提到的那样,XML Schema使用基于XML的语法,而DTD具有独特的语法。DTD不支持数据类型,这确实很重要。
让我们看一个非常简单的例子,其中大学有多个学生,每个学生都有两个元素"name"和"year"。请注意,我在我的代码中使用了“// -->”作为注释。
现在我将同时用DTD和XSD编写此示例。
XSD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE university[ // --> university as root element
<!ELEMENT university (student*)> // --> university has * = Multiple students
<!ELEMENT student (name,year)> // --> Student has elements name and year
<!ELEMENT name (#PCDATA)> // --> name as Parsed character data
<!ELEMENT year (#PCDATA)> // --> year as Parsed character data
]>
<university>
<student>
<name>
John Niel //---> I can also use an Integer,not good
</name>
<year>
2000 //---> I can also use a string,not good
</year>
</student>
</university>
XML模式定义语言(XSD)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name ="uniType"> //--> complex datatype uniType
<xsd:sequence>
<xsd:element ref="student" maxOccurs="unbounded"/> //--> has unbounded no.of students
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="stuType"> //--> complex datatype stuType
<xsd:sequence>
<xsd:element ref="name"/> //--> has element name
<xsd:element ref="year"/> //--> has element year
</xsd:sequence>
</xsd:complexType>
<xsd:element name="university" type="uniType"/> //--> university of type UniType
<xsd:element name="student" type="stuType"/> //--> student of type stuType
<xsd:element name="name" type="xsd:string"/> //--> name of datatype string
<xsd:element name="year" type="xsd:integer"/> //--> year of datatype integer
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<university>
<student>
<name>
John Niel
</name>
<year>
2000 //--> only an Integer value is allowed
</year>
</student>
</university>
DTD是XML之前的技术,因此本身不是有效的XML。这可能是XSD被发明的最大原因。
both specify elements, attributes, nesting, ordering, #occurences
XSD also has data types, (typed) pointers, namespaces, keys and more.... unlike DTD
此外,尽管XSD有点啰嗦,但它的语法是XML的扩展,使得学习起来更方便。
1
、0或1
、0或多个
,而XSD可以指定最小和最大数量。 - Jesse Chisholm一个不同之处在于,在DTD中,元素的内容模型完全由其名称确定,而与其在文档中出现的位置无关:
假设您想要有
person
的元素name
的子元素name
本身具有子元素first
和last
就像这样
<person>
<name>
<first></first>
<last></last>
</name>
</person>
city
元素也需要有一个名为“name”的子元素,则DTD要求该“name”元素必须同样具有first
和last
作为子元素。尽管city.name
不需要将first
和last
作为子元素。person
和city
声明name
子元素。因此,在这些上下文中为它们提供适当的内容模型。trang
进行转换。)相似之处:
DTD 和 Schemas 都执行相同的基本功能:
区别:
示例:
DTD:
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT employees (Efirstname, Elastname, Etitle, Ephone, Eemail)>
<!ELEMENT Efirstname (#PCDATA)>
<!ELEMENT Elastname (#PCDATA)>
<!ELEMENT Etitle (#PCDATA)>
<!ELEMENT Ephone (#PCDATA)>
<!ELEMENT Eemail (#PCDATA)>
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">
<xsd:element name="dataroot">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="employees" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="generated" type="xsd:dateTime"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="employees">
<xsd:annotation>
<xsd:appinfo>
<od:index index-name="PrimaryKey" index-key="Employeeid " primary="yes"
unique="yes" clustered="no"/>
<od:index index-name="Employeeid" index-key="Employeeid " primary="no" unique="no"
clustered="no"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Elastname" minOccurs="0" od:jetType="text"
od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Etitle" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Ephone" minOccurs="0" od:jetType="text"
od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Eemail" minOccurs="0" od:jetType="text"
od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Ephoto" minOccurs="0" od:jetType="text"
od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
DTD的目的是定义XML文档的结构。它通过列出合法元素来定义结构:
<!ATTLIST contact type CDATA #IMPLIED>
<!ELEMENT address1 ( #PCDATA)>
<!ELEMENT city ( #PCDATA)>
<!ELEMENT state ( #PCDATA)>
<!ELEMENT zip ( #PCDATA)>
XML模式可以让模式作者指定元素数量的数据必须是数字或更具体地说,是整数。在以下示例中,我使用了string
:
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="address1" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
DTD只包含两种数据类型,即CDATA和PCDATA。但在模式中,您可以使用编程语言中使用的所有原始数据类型,并且可以灵活定义自己的自定义数据类型。
构建模式的开发人员可以基于核心数据类型以及使用不同的运算符和修饰符创建自定义数据类型。
<size name='medium'/>
现在size
是一个元素;-) - U. Windl