DTD or XML Schema. Which one is better? [closed]

房东的猫 提交于 2019-11-28 06:17:19

From 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.

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.

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.

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 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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!