本文介绍了DTD与XSD定义的XML语言范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在以下命题:
每个DTD都有一个定义完全相同的语言的XSD,每个XSD都有一个定义完全相同的语言的DTD。或者换种说法:任何DTD定义的语言集合就是任何XSD定义的语言集合吗?

Does the following propositions hold:For every DTD there is an XSD that defines exactly the same language, and for every XSD there is a DTD that defines exactly the same language. Or put another way: The collection of languages defined by any DTD is exactly the the collection of languages defined by any XSD?

稍微扩展一下这个问题:XML文档基本上是一个大字符串。语言是字符串的集合。例如,所有MathML文档的(无限)集都是一种语言,所有RSS文档的集也是如此,依此类推。 MathML(RSS,...)也是所有XML文档(无限)集的适当子集。您可以使用DTD或XSD定义这样的XML子集。

Expanding on the question a little: An XML document is basically a large string. A language is a collection of strings. For example, the (infinite) set of all MathML documents is a language, and so is the set of all RSS documents and so on. MathML (RSS, ...) is also a proper subset of the (infinite) set of all XML documents. You can use DTD or XSD to define such a subset of XML.

现在,每个DTD都只定义一种语言。但是,如果您考虑所有可能的DTD,则会获得一组语言。我的问题是,此设置与您从所有可能的XSD中获得的设置完全相同吗?如果是这样,则从两者定义的XML语言的范围相等的意义上讲,DTD和XSD是等效的。

Now, every DTD defines exactly one language. But if you think of all possible DTDs, you get a set of languages. My question is, is this set exactly the same as the one you get from all possible XSDs? If so, then DTD and XSD are equivalent in the sense that the scope of XML languages defined by either is equal.

为什么这个问题很重要?如果DTD和XSD都相等,则可以编写一个以DTD作为输入并给您等效的XSD的程序,而另一个程序则相反。我知道有很多程序声称可以做到这一点,但是我不确定这是否真的可能。

Why is this question important? If both DTD and XSD are equivalent then it is possible to write a program that takes a DTD as input and gives you an equivalent XSD, and another program that does the opposite. I know there are quite a few programs out there that claim to do exactly this, but I'm in doubt whether or not that's actually possible.

推荐答案

一个有趣的问题;

在两个方向上的答案都是否。

The answer is "no", in both directions.

这是一个DTD,具有XSD中没有等效项:

Here is a DTD which has no equivalent in XSD:

<!ELEMENT e (#PCDATA | e)* >
<!ENTITY egbdf "Every good boy deserves favor.">

此DTD接受的字符序列集包括< e / > < ebdf;< / e> ,但不是< e>& ; beadgcf;< / e>

The set of character sequences accepted by this DTD includes both <e/> and <e>&egbdf;</e>, but not <e>&beadgcf;</e>.

由于XSD验证针对的是已扩展实体的信息集,因此没有XSD模式可以将第三种情况与第二种情况区分开。

Since XSD validation operates on an information set in which entities have all already been expanded, no XSD schema can distinguish the third case from the second.

DTD可以表达XSD中无法表达的约束的第二个方面涉及NOTATION类型。我不会举一个例子;细节太复杂了,以至于我无法在不查找的情况下正确地记住它们,并且不够有趣,以至于我不想这么做。

A second area where DTDs can express constraints not expressible in XSD involves NOTATION types. I won't give an example; the details are too complicated for me to remember them correctly without looking them up, and not interesting enough to make me want to do so.

第三个方面:DTD对待命名空间属性(即名称空间声明)和常规属性的使用方式相同;因此,DTD可以限制文档中名称空间声明的外观。 XSD模式不能。 xsi命名空间中的属性也是如此。

A third area: DTDs treat namespace attributes (aka namespace declarations) and general attributes in the same way; a DTD can therefore constrain the appearance of namespace declarations in documents. An XSD schema cannot. The same applies to attributes in the xsi namespace.

如果我们忽略所有这些问题,而仅针对不包含对其他命名实体的引用的字符序列提出问题比预定义的实体 lt gt 等,那么答案就改变了:对于每个不涉及NOTATION的DTD在声明中,有一个XSD架构,它在实体扩展后会接受完全相同的一组文档,并且以忽略名称空间属性和xsi名称空间中属性的方式定义了相同。

If we ignore all of those issues, and formulate the question with respect only to character sequences containing no references to named entities other than the pre-defined entities lt, gt, etc., then the answer changes: for every DTD not involving NOTATION declarations, there is an XSD schema that accepts precisely the same set of documents after entity expansion and with 'same' defined in a way that ignores namespace attributes and attributes in the xsi namespace.

从另一个方向看,差异领域包括:

In the other direction, the areas of difference include these:


  • XSD支持名称空间:以下XSD架构接受指定的目标名称空间中元素 e 的任何实例,无论文档实例中该名称空间绑定了什么前缀。

  • XSD is namespace aware: the following XSD schema accepts any instance of element e in the specified target namespace, regardless of what prefix is bound to that namespace in the document instance.

<xs:schema xmlns:xs="..." targetNamespace="http://example.com/nss/24397">
  <xs:element name="e" type="xs:string"/>
</xs:schema>

没有DTD可以成功接受全部,只有 e 给定名称空间中的元素。

No DTD can successfully accept all and only the e elements in the given namespace.

XSD具有一组更丰富的数据类型,可以使用数据类型来约束元素和属性。以下XSD架构没有等效的DTD:

XSD has a richer set of datatypes and can use datatypes to constrain elements as well as attributes. The following XSD schema has no equivalent DTD:

<xs:schema xmlns:xs="...">
  <xs:element name="e" type="xs:integer"/>
</xs:schema>

此架构接受文档< e> 42< / e> ,而不是文档< e> 42d Street< / e> 。没有DTD可以做到这一点,因为DTD没有约束#PCDATA内容的机制。最接近的DTD是<!ELEMENT e(#PCDATA)> ,它接受两个示例文档。

This schema accepts the document <e>42</e> but not the document <e>42d Street</e>. No DTD can make that distinction, because DTDs have no mechanism for constraining #PCDATA content. The closest DTD would be <!ELEMENT e (#PCDATA)>, which accepts both sample documents.

XSD的 xsi:type 属性允许对内容模型进行文档内修改。以下架构文档描述的XSD架构没有等效的DTD:

XSD's xsi:type attribute allows in-document modifications of content models. The XSD schema described by the following schema document has no equivalent DTD:

<xs:schema xmlns:xs="...">
  <xs:complexType name="e">
    <xs:sequence>
      <xs:element ref="e" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="e2">
    <xs:sequence>
      <xs:element ref="e" minOccurs="2" maxOccurs="2"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="e" type="e"/>
</xs:schema>

此架构接受文档< e xmlns:xsi =。 。 xsi:type = e2>< e />< e />< / e> 并拒绝文档< e xmlns: xsi = ... xsi:type = e2< e />< e />< e />< / e> 。 DTD没有使内容模型依赖于文档实例中给定的属性值的机制。

This schema accepts the document <e xmlns:xsi="..." xsi:type="e2"><e/><e/></e> and rejects the document <e xmlns:xsi="..." xsi:type="e2"><e/><e/><e/></e>. DTDs have no mechanism for making content models depend on an attribute value given in the document instance.

XSD通配符允许在格式之间包括任意格式正确的XML。指定元素的子元素;使用DTD时最接近的一种是使用<!ELEMENT e ANY> 形式的元素声明,这是不一样的,因为它需要声明所有出现的元素实际上都存在。

XSD wildcards allow the inclusion of arbitrary well-formed XML among the children of specified elements; the closest one can come to that with a DTD is to use an element declaration of the form <!ELEMENT e ANY>, which is not the same because it requires declarations for all the elements which in fact appear.

XSD 1.1提供了断言和条件类型分配,在DTD中没有类似物。

XSD 1.1 provides assertions and conditional type assignment, which have no analogues in DTDs.

XSD的表达能力可能超过DTD的表达能力,但我认为这一点已得到充分说明。

There are probably other ways in which the expressive power of XSD exceeds that of DTDs, but I think the point has been illustrated adequately.

我认为一个合理的总结是:XSD可以表达DTD可以表达的所有内容,但实体声明和特殊情况(例如名称空间声明和xsi:*属性)除外,因为XSD旨在能够做到这一点。因此,在将DTD转换为XSD架构文档时,信息丢失是相对适度的,易于理解的,并且主要涉及大多数词汇设计人员认为与DTD伪像无关的东西。

I think a fair summary would be: XSD can express everything DTDs can express, with the exception of entity declarations and special cases like namespace declarations and xsi:* attributes, because XSD was designed to be able to do so. So the loss of information when translating a DTD to an XSD schema document is relatively modest, well understood, and mostly involves things most vocabulary designers regard as DTD artefacts not of substantive interest.

XSD可以表达的内容比DTD可以表达的更多,这再次是因为XSD旨在这样做。在一般情况下,从XSD转换为DTD必然会导致信息丢失(接受的文档集可能需要更大或更小,或者是重叠的集)。关于如何管理信息丢失,可以做出不同的选择,这带来了一个问题:如何最好地将XSD转换为DTD形式?有一定的理论意义。 (但是,很少有人在实践中发现这是一个有趣的问题。)

XSD can express more than DTDs can, again because XSD was designed to do so. In the general case, translation from XSD to DTD necessarily involves loss of information (the set of documents accepted may need to be larger, or smaller, or to be an overlapping set). Different choices can be made about how to manage the loss of information, which gives the question "How does one best translate an XSD into DTD form?" a certain theoretical interest. (Very few people, however, seem to find it an interesting question in practice.)

所有这些都像您的问题一样,集中于字符序列的文档,语言是文档集,而模式语言是这种意义上的语言生成器。模式中存在的可维护性和信息问题不会导致文档集扩展方面的差异(例如,文档模型中类层次结构的处理)不予考虑。

All of this focuses, as did your question, on documents as character sequences, on languages as document sets, and on schema languages as generators of languages in that sense. Issues of maintainability and information present in the schema that does not turn into differences in the extension of document sets (e.g. the treatment of class hierarchies in the document model) is left out of account.

这篇关于DTD与XSD定义的XML语言范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:07