XML Schema 和 DTD 有什么区别? [英] What is difference between XML Schema and DTD?

查看:34
本文介绍了XML Schema 和 DTD 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

任何指导将不胜感激.

解决方案

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

<块引用>

DTD 之间的关键区别而 XML Schema 就是那个 XML Schema使用基于 XML 的语法,而DTD 有一个独特的语法来自 SGML DTD.虽然 DTD 是经常因为这种需要而受到批评学习新语法,语法本身很简洁.相反的是XML Schema 为真,它们是冗长,但也使用标签和XML 以便 XML 的作者可以找到XML Schema 的语法较少吓人.

DTD 的目标是保持一个水平与 SGML 的兼容性可能想要的应用程序将 SGML DTD 转换为 XML DTD.然而,为了与其中之一保持一致XML 的目标,XML 标记的简洁性是最不重要的,"没有真正关心保持语法简短.

[...]

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

打字

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

[...]

发生限制

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

[...]

枚举

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

<!ELEMENT 衬衫 (#PCDATA)><!ATTLIST 衬衫size_value(小 | 中 | 大)>

[...]

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

[...]

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.

Any guidance would be highly appreciated.

解决方案

From the Differences Between DTDs and Schema section of the Converting a DTD into a Schema article:

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 a simpleType called size_values which we can now use with an element:

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

[...]

这篇关于XML Schema 和 DTD 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆