DTD 或 XML 架构。哪一个更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1490583/
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
DTD or XML Schema. Which one is better?
提问by les2
What are the pros / cons in DTD and XML Schemas (I'm not even sure what the official name of the latter is!)? Which is better? Why do we need two ways to do the same thing?
DTD 和 XML 模式的优缺点是什么(我什至不确定后者的正式名称是什么!)?哪个更好?为什么我们需要两种方法来做同样的事情?
Edit: I found this in an article I was reading, which is what prompted me to ask the question:
编辑:我在我正在阅读的一篇文章中发现了这一点,这促使我提出这个问题:
Why W3C XML Schema Language?
The W3C XML Schema Language is not the only schema language. In fact, the XML specification describes document-type definitions (DTDs) as the way to express a schema. In addition, pre-release versions of the JAXB Reference Implementation worked only with DTDs -- that is, not with schemas written in the XML Schema Language. However, the XML Schema Language is much richer than DTDs. For example, schemas written in the XML Schema Language can describe structural relationships and data types that can't be expressed (or can't easily be expressed) in DTDs. There are tools available to convert DTDs to the W3C XML Schema Language, so if you have DTD-based schemas that you used with an earlier version of the JAXB Reference Implementation, you can use these tools to convert the schemas to XML Schema Language. http://java.sun.com/developer/technicalArticles/WebServices/jaxb/#binsch
为什么选择 W3C XML 模式语言?
W3C XML 模式语言不是唯一的模式语言。事实上,XML 规范将文档类型定义 (DTD) 描述为表达模式的方式。此外,JAXB 参考实现的预发布版本仅适用于 DTD —— 也就是说,不适用于用 XML 模式语言编写的模式。但是,XML Schema Language 比 DTD 丰富得多。例如,用 XML 模式语言编写的模式可以描述在 DTD 中无法表达(或不容易表达)的结构关系和数据类型。有一些工具可用于将 DTD 转换为 W3C XML 模式语言,因此如果您有基于 DTD 的模式,并且与早期版本的 JAXB 参考实现一起使用,http://java.sun.com/developer/technicalArticles/WebServices/jaxb/#binsch
I guess I would like examples that illustrate why XML-Schema is better (if it indeed is).
我想我想要一些例子来说明为什么 XML-Schema 更好(如果确实如此)。
采纳答案by Robert Harvey
From http://weblogs.asp.net/rchartier/archive/2006/03/21/440782.aspx
来自http://weblogs.asp.net/rchartier/archive/2006/03/21/440782.aspx
DTD's are not namespace aware.
DTD's have
#define,#include, and#ifdef-- or, less C-oriented, the ability to define shorthand abbreviations, external content, and some conditional parsing.A DTD describes the entire XML document (even if it leaves "holes"); a schema can define portions.
XSD has a type system.
XSD has a much richer language for describing what element or attribute content "looks like." This is related to the type system.
You can put a DTD inline into an XML document, you cannot do this with XSD. This means DTD's are more secure (you only have to protect one bytestream -- the xml/dtd -- and not
multiple).The official definition of "valid XML" requires a DTD. Since this may be impractical, if not impossible, you often have to settle for schema-valid, which is not quite the same.
DTD 不知道名称空间。
DTD 具有
#define、#include和#ifdef-- 或者,较少面向 C 语言,能够定义速记缩写、外部内容和一些条件解析。DTD 描述了整个 XML 文档(即使它留下了“漏洞”);模式可以定义部分。
XSD 有一个类型系统。
XSD 有更丰富的语言来描述元素或属性内容“看起来像什么”。这与类型系统有关。
您可以将 DTD 内嵌到 XML 文档中,而使用 XSD 则无法做到这一点。这意味着 DTD 更安全(您只需保护一个字节流——xml/dtd——而不是
多个)。“有效 XML”的官方定义需要 DTD。由于这可能是不切实际的,如果不是不可能的话,您通常不得不满足于模式有效,这并不完全相同。
For my part, it's pretty straightforward to write a validator for some XML if you have an XSD. I haven't seen this with a DTD, although I'm sure it exists.
就我而言,如果您有 XSD,则为某些 XML 编写验证器非常简单。我没有在 DTD 中看到过这个,尽管我确信它存在。
回答by Pascal Sartoretti
A few years ago, there would be reasons to use DTD over XML Schema (it was more common or better supported by XML tools). Today, however, I see no reason to not use XML Schema instead of DTD : XML Schema is much more powerful.
几年前,有理由在 XML 模式上使用 DTD(它更常见或由 XML 工具更好地支持)。然而,今天,我认为没有理由不使用 XML Schema 而不是 DTD:XML Schema 的功能要强大得多。
However, XML Schema is far from being perfect (just try to read the spec or a book on XML Schema...) and many alternatives have been developed since then (Schematron, Examplotron, RelaxNG). These may have technical advantages over XML Schema, but XML Schema is so much more pervasive today that I see very few cases where an alternative would make sense.
然而,XML Schema 远非完美(只需尝试阅读有关 XML Schema 的规范或书籍......)并且从那时起已经开发了许多替代方案(Schematron、Examplotron、RelaxNG)。这些可能比 XML Schema 具有技术优势,但是 XML Schema 在今天更加普遍,我看到很少有替代方案有意义的情况。
回答by Aruna
XML Schema can perform more complex validations. For example if DTD can check if the datatype of an XML element is integer or string. Whereas XML schema can perform more complicated validations like if the xml element is a string starting with uppercase letter or a positve 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 服务的自然选择。
回答by Sergey Stolyarov
There is also Relax NG — another powerful language for validating XML documents, along with Schematron and other technologies from DSDL. Relax NG is very simple and have human readable form — Relax NG Compact that allows scheme writing similar to BNF schemes.
还有 Relax NG——另一种用于验证 XML 文档的强大语言,以及来自DSDL 的Schematron 和其他技术。Relax NG 非常简单并且具有人类可读的形式——Relax NG Compact 允许类似于 BNF 方案的方案编写。

