XML Schema 和 DTD 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1544200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:48:00  来源:igfitidea点击:

What is difference between XML Schema and DTD?

xmlschemadtd

提问by Rachel

I have googled this question, but I do not understand clearly what is an XML schema and DTD (document type definition), and why the XML schema is more powerful compared to DTD.

我用谷歌搜索过这个问题,但我不清楚什么是 XML 模式和 DTD(文档类型定义),以及为什么 XML 模式比 DTD 更强大。

Any guidance would be highly appreciated.

任何指导将不胜感激。

采纳答案by Pascal Thivent

From the Differences Between DTDs and Schemasection of the Converting a DTD into a Schemaarticle:

将 DTD 转换为架构文章的DTD 和架构之间差异部分:

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 sizeto 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 a simpleTypecalled size_valueswhich we can now use with an element:

<xs:element name="size" type="size_value">

[...]

DTD 和 XML Schema 之间的关键区别在于 XML Schema 使用基于 XML 的语法,而 DTD 具有保留自 SGML DTD 的独特语法。尽管 DTD 经常因为需要学习新语法而受到批评,但语法本身非常简洁。XML Schema 恰恰相反,它很冗长,但也使用了标记和 XML,因此 XML 的作者应该发现 XML Schema 的语法不那么令人生畏。

DTD 的目标是为可能希望将 SGML DTD 转换为 XML DTD 的应用程序保留与 SGML 的兼容性级别。然而,为了与 XML 的目标之一保持一致,“XML 标记的简洁性是最不重要的”,因此并没有真正关心保持语法简短。

[...]

那么,当我们转换 DTD 时,还有哪些其他差异可能特别重要?让我们来看看。

打字

DTD 和 XML Schema 之间最显着的区别是能够在 Schema 中结合元素和属性声明创建和使用数据类型。事实上,这是一个非常重要的区别,以至于 XML Schema Recommendation 的一半专门用于数据类型和 XML Schema。我们在本书的第三部分“XML 模式数据类型”中详细介绍了数据类型。

[...]

发生约束

DTD 和 Schema 显着不同的另一个领域是出现约束。如果您还记得我们之前在第 2 章“模式结构”(或您自己使用 DTD 的工作)中的示例,您可以使用三个符号来限制元素出现的次数:*、+ 和 ?。

[...]

枚举

所以,假设我们有一个元素,我们希望能够为衬衫定义一个尺寸属性,允许用户选择尺寸:小、中或大。我们的 DTD 看起来像这样:

<!ELEMENT item (shirt)>
<!ELEMENT shirt (#PCDATA)>
<!ATTLIST shirt
    size_value (small | medium | large)>

[...]

但是如果我们想size成为一个元素呢?我们不能用 DTD 做到这一点。DTD 不提供元素文本内容中的枚举。然而,由于 Schema 的数据类型,当我们在前面的示例中声明枚举时,我们实际上创建了一个simpleType调用size_values,我们现在可以将其与元素一起使用:

<xs:element name="size" type="size_value">

[...]

回答by tanuja nanda

Differences between an XML Schema Definition (XSD) and Document Type Definition (DTD) include:

XML 架构定义 (XSD) 和文档类型定义 (DTD) 之间的差异包括:

  • XML schemas are written in XML while DTD are derived from SGML syntax.
  • XML schemas define datatypes for elements and attributes while DTD doesn't support datatypes.
  • XML schemas allow support for namespaces while DTD does not.
  • XML schemas define number and order of child elements, while DTD does not.
  • XML schemas can be manipulated on your own with XML DOM but it is not possible in case of DTD.
  • using XML schema user need not to learn a new language but working with DTD is difficult for a user.
  • XML schema provides secure data communication i.e sender can describe the data in a way that receiver will understand, but in case of DTD data can be misunderstood by the receiver.
  • XML schemas are extensible while DTD is not extensible.
  • XML 模式是用 XML 编写的,而 DTD 是从 SGML 语法派生的。
  • XML 模式定义元素和属性的数据类型,而 DTD 不支持数据类型。
  • XML 模式允许支持命名空间,而 DTD 则不允许。
  • XML 模式定义了子元素的数量和顺序,而 DTD 没有。
  • XML 模式可以使用 XML DOM 自行操作,但在 DTD 的情况下是不可能的。
  • 使用 XML 模式的用户不需要学习新语言,但使用 DTD 对用户来说很困难。
  • XML 模式提供安全的数据通信,即发送方可以以接收方能够理解的方式描述数据,但在 DTD 的情况下,接收方可能会误解数据。
  • XML 模式是可扩展的,而 DTD 是不可扩展的。

UPDATE: 2015.08.26

更新:2015.08.26

Not all these bullet points are 100% accurate, but you get the gist.

并非所有这些要点都是 100% 准确的,但您明白了要点。

On the other hand:

另一方面:

  • DTD lets you define new ENTITYvalues for use in your XML file.
  • DTD lets you extend it local to an individual XML file.
  • DTD 允许您定义新的ENTITY值以在您的 XML 文件中使用。
  • DTD 允许您将其本地扩展到单个 XML 文件。

回答by N Randhawa

As many people have mentioned before, XML Schema utilize an XML-based syntax and DTDs have a unique syntax. DTD doesn't support datatypes, which does matter.

正如许多人之前提到的,XML Schema 使用基于 XML 的语法,而 DTD 具有独特的语法。DTD 不支持数据类型,这很重要。

Lets see a very simple example in which university has multiple students and each student has two elements "name" and "year". Please note that I have uses "// --> " in my code just for comments.

让我们看一个非常简单的例子,其中大学有多个学生,每个学生有两个元素“姓名”和“年份”。请注意,我在代码中使用了 "// --> " 只是为了评论。

enter image description here

在此处输入图片说明

Now I will write this example both in DTD and in XSD.

现在我将在 DTD 和 XSD 中编写此示例。

DTD

DTD

<?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 Schema Definition (XSD)

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>

回答by troelskn

DTD predates XML and is therefore not valid XML itself. That's probably the biggest reason for XSD's invention.

DTD 早于 XML,因此它本身不是有效的 XML。这可能是 XSD 发明的最大原因。

回答by krishna_kp

Similarities between XSD and DTD

XSD 和 DTD 之间的相似之处

both specify elements, attributes, nesting, ordering, #occurences

Differences between XSD and DTD

XSD 和 DTD 之间的差异

XSD also has data types, (typed) pointers, namespaces, keys and more.... unlike DTD 

Moreover though XSD is little verbose its syntax is extension of XML, making it convenient to learn fast.

此外,虽然XSD 有点冗长,但它的语法是XML 的扩展,便于快速学习。

回答by JaakkoK

One difference is also that in a DTD, the content model of an element is completely determined by its name, independently of where it appears in the document. So, say you want to have a namechild element of your personelement that itself has child elements firstand last. Then if you wanted to have a namechild element for a cityelement in the same document, that would also need to have child elements firstand last. In contrast, XML Schema allows you to declare child element types locally, so in this case you could declare the namechild elements for both personand cityseparately, giving them their proper content models in those contexts.

一个区别还在于,在 DTD 中,元素的内容模型完全由其名称决定,而与其出现在文档中的位置无关。因此,假设您希望name元素的person子元素本身具有子元素firstlast. 然后,如果您想为同一文档中namecity元素创建子元素,则还需要有子元素firstlast. 相比之下,XML Schema 允许您在本地声明子元素类型,因此在这种情况下,您可以name为两者personcity单独声明子元素,在这些上下文中为它们提供适当的内容模型。

The other major difference is support for namespaces. Since DTDs are part of the original XML specification (and inherited from SGML), they are not namespace-aware at all because XML namespaces were specified later. You can use DTDs in combination with namespaces, but it requires some contortions, like being forced to define the prefixes in the DTD and using only those prefixes, instead of being able to use arbitrary prefixes.

另一个主要区别是对命名空间的支持。由于 DTD 是原始 XML 规范的一部分(并从 SGML 继承),它们根本不知道名称空间,因为稍后指定了 XML 名称空间。您可以将 DTD 与名称空间结合使用,但它需要一些扭曲,例如被迫在 DTD 中定义前缀并仅使用这些前缀,而不是能够使用任意前缀。

To me, other differences are mostly superficial. Datatype support could easily be added to DTDs, and syntax is just syntax. (I, for one, find the XML Schema syntax horrible and would never want to hand-maintain an XML Schema, which I wouldn't say about DTDs or RELAX NG schemas; if I need an XML Schema for some reason, I usually write a RELAX NG one and convert it with trang.)

对我来说,其他差异大多是肤浅的。数据类型支持可以很容易地添加到 DTD 中,语法只是语法。(一方面,我发现 XML Schema 语法很糟糕,并且永远不想手动维护 XML Schema,我不会说 DTD 或 RELAX NG 模式;如果我出于某种原因需要 XML Schema,我通常会写一个 RELAX NG 并用trang.)

回答by ACV

Similarities:

相似之处

DTDs and Schemas both perform the same basic functions:

DTD 和模式都执行相同的基本功能:

  • First, they both declare a laundry list of elements and attributes.
  • Second, both describe how those elements are grouped, nested or used within the XML. In other words, they declare the rules by which you are allowing someone to create an XML file within your workflow, and
  • Third, both DTDs and schemas provide methods for restricting, or forcing, the type or format of an element. For example, within the DTD or Schema you can force a date field to be written as 01/05/06 or 1/5/2006.
  • 首先,它们都声明了一个元素和属性的清单。
  • 其次,两者都描述了如何在 XML 中对这些元素进行分组、嵌套或使用。换句话说,它们声明了允许某人在您的工作流中创建 XML 文件的规则,并且
  • 第三,DTD 和模式都提供了限制或强制元素类型或格式的方法。例如,在 DTD 或 Schema 中,您可以强制将日期字段写为 01/05/06 或 1/5/2006。

Differences:

区别:

  • DTDs are better for text-intensive applications, while schemas have several advantages for data-intensive workflows.

  • Schemas are written in XML and thusly follow the same rules, while DTDs are written in a completely different language.

  • DTD 更适合文本密集型应用程序,而模式对于数据密集型工作流有几个优势。

  • 模式是用 XML 编写的,因此遵循相同的规则,而 DTD 是用完全不同的语言编写的。

Examples:

例子:

DTD:

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:

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>

回答by Nirav Daraniya

DTD can have only two types of data, the CDATA and the PCDATA. But in a schema you can use all the primitive data type that you use in the programming language and you have the flexibility of defining your own custom data types.

DTD 只能有两种类型的数据,CDATA 和 PCDATA。但是在模式中,您可以使用在编程语言中使用的所有原始数据类型,并且可以灵活地定义自己的自定义数据类型。

The developer building a schema can create custom data types based on the core data types and by using different operators and modifiers.

构建模式的开发人员可以根据核心数据类型并使用不同的运算符和修饰符来创建自定义数据类型。

回答by Robert

When XML first came out, we were told it would solve all our problems: XML will be user-friendly, infinitely extensible, avoid strong-typing, and not require any programming skills. I learnt about DTD's and wrote my own XML parser. 15+ years later, I see that most XML is not user-friendly, and not very extensible (depending on its usage). As soon as some clever clogs hooked up XML to a database I knew that data types were all but inevitable. And, you should see the XSLT (transformation file) I had to work on the other day. If that isn't programming, I don't know what is! Nowadays it's not unusual to see all kinds of problems relating to XML data or interfaces gone bad. I love XML but, it has strayed far from its original altruistic starting point.

当 XML 首次出现时,我们被告知它将解决我们所有的问题:XML 将是用户友好的、无限可扩展的、避免强类型并且不需要任何编程技能。我了解了 DTD 并编写了自己的 XML 解析器。15 多年后,我发现大多数 XML 不是用户友好的,并且不是很可扩展(取决于它的用法)。一旦一些聪明的木屐将 XML 连接到数据库,我就知道数据类型几乎是不可避免的。而且,您应该会看到我前几天必须工作的 XSLT(转换文件)。如果那不是编程,我不知道什么是!如今,看到与 XML 数据或接口相关的各种问题变坏的情况并不少见。我喜欢 XML,但是,它已经偏离了它最初的利他主义起点。

The short answer? DTD's have been deprecated in favor of XSD's because an XSD lets you define an XML structure with more precision.

简短的回答?DTD 已被弃用,取而代之的是 XSD,因为 XSD 使您可以更精确地定义 XML 结构。

回答by Doro

XML DTD

XML DTD

The purpose of a DTDis to define the structure of an XML document. It defines the structure with a list of legal elements:

DTD的目的是定义 XML 文档的结构。它定义了具有合法元素列表的结构:

<!ATTLIST contact type CDATA #IMPLIED>
<!ELEMENT address1 ( #PCDATA)>
<!ELEMENT city ( #PCDATA)>
<!ELEMENT state ( #PCDATA)>
<!ELEMENT zip ( #PCDATA)>

XML Schema

XML 架构

XML Schemaenables schema authors to specify that element quantity's data must be numeric or, even more specifically, an integer. In the following example I used string:

XML Schema使模式作者能够指定元素数量的数据必须是数字,或者更具体地说,是整数。在以下示例中,我使用了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>