xml 如何在 DTD 和 XSD 之间进行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2389760/
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 13:00:43  来源:igfitidea点击:

How to choose between DTD and XSD

xmlxsddtd

提问by ajsie

I want to use either a DTD or an XSD to describe my XML document. I've read that XSDs are better than DTDs since they support namespaces and data types, and that DTDs are older.

我想使用 DTD 或 XSD 来描述我的 XML 文档。我读到 XSD 比 DTD 更好,因为它们支持命名空间和数据类型,而且 DTD 更旧。

Does this mean that I should only use XSDs for all future needs and totally ignore DTD as an option? Should I even bother learning the structure of DTDs?

这是否意味着我应该只使用 XSD 来满足未来的所有需求,而完全忽略 DTD 作为一种选择?我是否应该费心学习 DTD 的结构?

What factors should I consider when choosing between XSD and DTD?

在 XSD 和 DTD 之间进行选择时应该考虑哪些因素?

采纳答案by Mike Mooney

It's probably important to learn DTDs as a separate exercise, just for the knowledge of how they work in case you encounter them somewhere else, and so that you can appreciate some of the things that XSD was trying to solve.

作为一个单独的练习来学习 DTD 可能很重要,只是为了了解它们是如何工作的,以防您在其他地方遇到它们,并且这样您就可以了解 XSD 试图解决的一些问题。

However, for your current purposes of describing an XML document, indeed stick to XSDs.

但是,就您目前描述 XML 文档的目的而言,确实要坚持使用 XSD。

In addition to having a far richer feature set (like you mention, including data types and namespaces), they are also XML documents themselves, which can be really useful. Because they are XML, you can check their well-formedness and validity a lot easier, and you can write code that works with them like regular XML files (for instance, if you wanted to autogenerate code classes from a schema)

除了拥有更丰富的功能集(如您提到的,包括数据类型和名称空间)之外,它们本身也是 XML 文档,这非常有用。因为它们是 XML,所以您可以更轻松地检查它们的格式正确性和有效性,并且您可以编写像常规 XML 文件一样使用它们的代码(例如,如果您想从模式自动生成代码类)

回答by Brant

It really depends on how complicated the structure is that you need to setup.

这实际上取决于您需要设置的结构有多复杂。

If you need things like namespacing and datatypes, definitely go with XSD. If you just need a quick little schema to check against, DTD will give you faster performance since there is no XML parsing involved.

如果您需要诸如命名空间和数据类型之类的东西,请务必使用 XSD。如果您只需要一个快速的小模式来检查,DTD 将为您提供更快的性能,因为不涉及 XML 解析。

As I understand it, XSD is derived from DTD so understanding DTD will give a solid foundation for learning XSD, plus point out some of DTD's short comings.

据我了解,XSD 是从 DTD 派生出来的,所以理解 DTD 将为学习 XSD 打下坚实的基础,并指出 DTD 的一些缺点。

回答by Justin Niessner

It wouldn't hurt to understand the structure of a DTD (it'll help you better understand an XSD in the long run)...but you should use XSDs moving forward.

理解 DTD 的结构不会有什么坏处(从长远来看,它会帮助您更好地理解 XSD)……但是您应该继续使用 XSD。

回答by InfantPro'Aravind'

No harm in learning DTD, but be sure to use XSD, because XSD has more strength,

学习DTD没有坏处,但是一定要使用XSD,因为XSD的实力更强,

With XSD you can not only validate the structure/hierarchy of the XML tags but also,

使用 XSD,您不仅可以验证 XML 标签的结构/层次结构,而且还可以,

  1. You can define Data type of the values of the nodes. [date, number, string etc]
  2. You can also define custom data_types, [example, for node , the possible data can be one of the 12 months.. so you need to define all the 12 months in a new data type writing all the 12 month names as enumeration values .. validation shows error if the input XML contains any-other value than these 12 values .. ]
  3. You can put the restriction on the occurrence of the elements, using minOccurs and maxOccurs, the default values are 1 and 1.
  1. 您可以定义节点值的数据类型。[日期、数字、字符串等]
  2. 您还可以定义自定义 data_types,[例如,对于 node ,可能的数据可以是 12 个月之一。因此您需要在新数据类型中定义所有 12 个月,将所有 12 个月的名称写入枚举值 ..如果输入 XML 包含除这 12 个值之外的任何其他值,则验证会显示错误..]
  3. 可以对元素的出现次数进行限制,使用 minOccurs 和 maxOccurs,默认值为 1 和 1。

.. and many more ...

.. 还有很多 ...

There are some restrictions: as like,

有一些限制:例如,

  1. An element(name) defined in XSD file must be defined with only one data-type.
  2. You can't validate a node/attribute using the value of another node/attribute.
  1. XSD 文件中定义的元素(名称)必须仅定义为一种数据类型。
  2. 您不能使用另一个节点/属性的值来验证一个节点/属性。

回答by MikeTheGuru

There is an IMHO very important issue to use a DTD (maybe together with a XSD if you need in-deep-validation):

恕我直言,使用 DTD 有一个非常重要的问题(如果您需要深入验证,可以与 XSD 一起使用):

In DTD you can define your own entities eg:

在 DTD 中,您可以定义自己的实体,例如:

<!ENTITY MyName "DrDr.Hannibal Xerxes Utah,MBA and CEO">

In your document you can wherevever needed simply code &MyName; instead typing all this stuff.

在您的文档中,您可以在任何需要的地方简单地编码 &MyName; 而是输入所有这些东西。

Furthermore assume you have a XML-like file (maybe produced by some other application) that consists of a lot of similar tags but no root-tag, eg:

此外,假设您有一个类似 XML 的文件(可能由其他应用程序生成),其中包含许多类似的标签但没有根标签,例如:

<?xml version="1.0" encoding="ISO-8859-1"?> <!-- you need this when using foreign characters like 'ü' -->
<Book Author="Author1">
  <Titel>Erstes Buch</Titel>
</Book>
...
<Book Author="Author5">
  <Titel>Fünftes Buch</Titel>
</Book>

Assume this file is named "Booklist.TXT",

假设这个文件名为“Booklist.TXT”,

Now you can code your master-xml:

现在您可以编写您的 master-xml 代码:

<?xml version="1.0" encoding="ISO-8859-1"?> <!-- you need this when using foreign characters like 'ü' -->
<DOCTYPE MyRoot [
<ENTITY AllBooks SYSTEM "Booklist.TXT">
]

<MyRoot>
... some prefix-stuff as needed ...
&AllBooks; <!-- here are all the Books -->
... some post stuff es needed ...
</MyBook>

and whenever you need the books in another context you only must code the surrounding xml and habe not to touch or copy the booklist itself, furthermore you can maintenance it in one single place and have all changes in any document.

并且每当您在另一个上下文中需要书籍时,您只需编写周围的 xml 代码,并且不得触摸或复制书单本身,此外,您可以在一个地方维护它,并在任何文档中进行所有更改。

回答by Joel

This is an old string, BUT in case anyone else comes across it... from what I can tell DTD still has two benefits which XSD does not, namely the inclusion of the ENTITY function which does not exist in XSD. This is a pretty awesome feature which tells the compiler how to process potentially unfamiliar file types by identifying what programs to open to process them.

这是一个旧字符串,但以防其他人遇到它......据我所知,DTD 仍然有两个 XSD 没有的好处,即包含 XSD 中不存在的 ENTITY 函数。这是一个非常棒的功能,它通过识别打开哪些程序来处理它们,告诉编译器如何处理可能不熟悉的文件类型。

Also, DTDs are written into the XML spec so they can be written directly into XML documents whereas XSD has to exist as an outside file and connected. Not a big deal especially when using in bigger documents anyway.

此外,DTD 被写入 XML 规范,因此它们可以直接写入 XML 文档,而 XSD 必须作为外部文件存在并连接。没什么大不了的,尤其是在更大的文档中使用时。

I think XSD is still far better and more natural since it uses XML syntax, just wanted to play devil's advocate :)

我认为 XSD 仍然更好更自然,因为它使用 XML 语法,只是想扮演恶魔的拥护者:)

回答by Rohit Goyal

XML Schema can perform more complex validations. E.g if DTD checks the datatype of an XML element is integer or string.XML schema can perform more complicated validations like if the xml element is a string starting with uppercase letter or a positive integer. Finally XML schema uses XML syntax and its a natural choice for development of web services.

XML Schema 可以执行更复杂的验证。例如,如果 DTD 检查 XML 元素的数据类型是整数还是字符串。XML 模式可以执行更复杂的验证,例如 xml 元素是否是以大写字母或正整数开头的字符串。最后,XML 模式使用 XML 语法,这是开发 Web 服务的自然选择。