简体   繁体   English

为每个 XML 放宽模式和 XML 模式模式?

[英]Relaxng schema and XML Schema schema for every XML?

Is there a set of XML documents for which we can formulate a RNG, but not an XML Schema (or the other way round)?是否有一组 XML 文档,我们可以为其制定 RNG,但不能制定 XML 模式(或相反)? Can you give me an example, please?你能举个例子吗?

As the OP's exchange with MK has shown, there is some opportunity for misunderstanding here.正如 OP 与 MK 的交流所表明的那样,这里存在一些误解的机会。 So I'll offer a response here even though MK has in fact already answered the question.因此,即使 MK 实际上已经回答了这个问题,我也会在这里做出回应。

I assume the question is whether XSD and RNG have the same expressive power, and if not whether one is more expressive than the other.我假设问题是 XSD 和 RNG 是否具有相同的表达能力,如果不是,是否一个比另一个更具表达力。

That is to say: every schema defines a set of documents (namely, the set valid against the schema).也就是说:每个模式都定义了一组文档(即对模式有效的集合)。 There are (as MK says) a great many sets of documents for which neither language can define a schema that accepts as valid just the documents in that set.有很多文档集(正如 MK 所说),两种语言都不能为其定义一个仅接受该集合中的文档为有效的模式。 So perhaps the clearest way to put the question is:所以也许最清楚的提出问题的方式是:

Given a schema in schema language L1, is there guaranteed to be a schema in schema language L2 that accepts as valid the same set of input documents?给定模式语言 L1 中的模式,是否保证模式语言 L2 中的模式接受相同的输入文档集为有效?

Or equivalently:或者等价地:

Are there schemas expressible in language L1 which have no equivalent in language L2?是否存在可用 L1 语言表达但在 L2 语言中没有等价物的模式?

The answer is that each of the schema languages mentioned can express some schemas not expressible with the other.答案是所提到的每种模式语言都可以表达一些其他模式无法表达的模式。

I'll leave aside trivia like xsi:type and possible peculiarities of the way Relax NG uses XSD simple types and questions of what exactly we mean by the set of documents accepted by a schema, though these points may be important in some contexts.我将撇开像xsi:type这样的琐事和 Relax NG 使用 XSD 简单类型的方式的可能特性,以及我们所说的模式接受的文档集究竟意味着什么的问题,尽管这些要点在某些上下文中可能很重要。

RelaxNG schemas with no XSD equivalents:没有 XSD 等价物的 RelaxNG 模式:

  • As MK points out, RelaxNG schemas can control the location of non-whitespace character data: (a, b, text, c, d) is a legitimate context model in Relax NG that has no equivalent in XSD.正如 MK 指出的那样,RelaxNG 模式可以控制非空白字符数据的位置: (a, b, text, c, d)是 Relax NG 中的合法上下文模型,在 XSD 中没有等效项。

    So as an example of a Relax NG schema (in compact syntax) with no equivalent in XSD, consider the following:因此,作为在 XSD 中没有等效项的 Relax NG 模式(采用紧凑语法)的示例,请考虑以下内容:

     start = ee = element e { (e, text, e)? }
  • XSD content models must be deterministic (in the jargon of the XSD spec, they must obey the Unique Particle Attribution constraint) while Relax NG content models need not be so. XSD 内容模型必须是确定性的(在 XSD 规范的行话中,它们必须服从唯一粒子属性约束),而 Relax NG 内容模型不需要如此。 So the possible sequences of moves for a chess game is expressible in Relax NG, but not in XSD: (white, black)*, white?因此,国际象棋游戏的可能移动顺序可以在 Relax NG 中表达,但不能在 XSD 中表达: (white, black)*, white? . . (Since every non-deterministic FSA has a deterministic equivalent, it sometimes surprises people that the same is not true for content models: it is not the case that every non-deterministic content model can be rewritten as an equivalent deterministic content model. Anne Brüggemann-Klein identified the set of regular languages for which there are no deterministic content models a couple of decades ago in her Habilitationsschrift .) (由于每个非确定性 FSA 都有一个确定性等价物,有时人们会惊讶于内容模型并非如此:并非每个非确定性内容模型都可以重写为等效的确定性内容模型。Anne Brüggemann -Klein 几十年前在她的Habilitationsschrift中确定了一组没有确定性内容模型的常规语言。)

     start = element game { (white, black)*, white? } white = element white {sq} black = element black {sq} sq = attribute square { text }
  • Since Relax NG includes attributes in content models, it is possible in Relax NG to make the effective content model of an element depend on the value of one of its attributes, in ways that are possible in XSD 1.1 but not in XSD 1.0.由于 Relax NG 在内容模型中包含属性,因此在 Relax NG 中可以使元素的有效内容模型依赖于其属性之一的值,这种方式在 XSD 1.1 中是可能的,但在 XSD 1.0 中是不可能的。

     start = element whaddyawant { (attribute gimme { 'a' }, a+) | (attribute gimme { 'b' }, b+) } a = element a { empty } b = element b { empty }

XSD schemas with no RelaxNG equivalents:没有 RelaxNG 等价物的 XSD 模式:

  • XSD defines types to support the ID/IDREF constraints expressible in XML DTDs; XSD 定义类型以支持可在 XML DTD 中表达的 ID/IDREF 约束; Relax NG relegates them to a separate 'DTD compatibility' which is (at least in my experience) awkward and error prone in practice. Relax NG 将它们降级为单独的“DTD 兼容性”,这在实践中(至少以我的经验)是笨拙且容易出错的。 In particular, if a schema declares an attribute of type ID, any wildcard element with wildcard attributes is likely to cause trouble.特别是,如果一个模式声明了一个 ID 类型的属性,任何带有通配符属性的通配符元素都可能引起麻烦。

  • XSD schemas can define uniqueness, key, and key reference constraints that are not expressible in Relax NG schemas. XSD 模式可以定义在 Relax NG 模式中无法表达的唯一性、键和键引用约束。 Example:例子:

     <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="digraph" type="DG"> <xs:key name="node-ids"> <xs:selector xpath="node"/> <xs:field xpath="@nodeID"/> </xs:key> <xs:keyref refer="node-ids" name="arc-ends"> <xs:selector xpath="node"/> <xs:field xpath="@arc-to"/> </xs:keyref> </xs:element> <xs:element name="node" type="N"/> <xs:complexType name="DG"> <xs:sequence> <xs:element ref="node" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="N"> <xs:attribute name="nodeID" type="xs:integer"/> <xs:attribute name="arc-to" type="xs:integer" use="optional"/> </xs:complexType> </xs:schema>
  • XSD 1.1 assertions have no analog in Relax NG, and it is possible to express constraints with them that cannot be expressed in Relax NG. XSD 1.1 断言在 Relax NG 中没有类似物,并且可以用它们来表达在 Relax NG 中无法表达的约束。 Eg 'in each district element, the value in total/@n must be equal to the sum of the n attributes on the other children ( number(total/@n) eq sum((* except total)/@n) ).例如'在每个district元素中, total/@n中的值必须等于其他子元素的n属性的总和( number(total/@n) eq sum((* except total)/@n) )。 Example left as an exercise for the reader.示例留给读者作为练习。

Note that MK is not quite right to say "XSD can define more precise rules on the cardinality range permitted for child elements".请注意,MK 说“XSD 可以为子元素允许的基数范围定义更精确的规则”并不完全正确。 I do not believe there is any cardinality constraint expressible in XSD that cannot be expressed in Relax NG.我不相信有任何可以在 XSD 中表达而不能在 Relax NG 中表达的基数约束。 It is true that it would be rather tedious to express in Relax NG a constraint that says there must be at least one line on an invoice, but not more than 999 lines.确实,在 Relax NG 中表达一个约束,即发票上必须至少有一行,但不超过 999 行,这确实是相当乏味的。 But it would certainly be possible.但这肯定是可能的。 A content model that says there must be at least one but not more than nine a elements is (a, a?, a?, a?, a?, a?, a?, a? a?) .表示必须至少有一个但不超过九个a元素的内容模型是(a, a?, a?, a?, a?, a?, a?, a? a?) It's easy to see how to extend that to handle the case of 999 lines on an invoice.很容易看出如何扩展它来处理发票上 999 行的情况。

When you talk of "a set of XML documents for which we can formulate a schema", do you mean that the schema must accept every document within the set, and reject every document outwith the set?当您谈到“我们可以为其制定模式的一组 XML 文档”时,您的意思是该模式必须接受该集合内的每个文档,并拒绝该集合外的每个文档吗? In general if you start with an arbitrary set of documents it's very unlikely that you will be able to formulate such a schema, regardless of your choice of schema language.一般来说,如果您从任意一组文档开始,那么无论您选择何种模式语言,您都不太可能制定出这样的模式。 And it's certainly true that the sets of documents that do have that property will be different from one schema language to another.的确,具有该属性的文档集在一种模式语言与另一种模式语言之间是不同的。

Moreover, if your set of documents is finite, then it's not really very useful to define such a schema, because it will be impossible to write any new documents that conform to the schema.此外,如果您的文档集是有限的,那么定义这样的模式实际上并不是很有用,因为不可能编写任何符合该模式的新文档。 While if the set of documents is infinite, then the only real way to define your set of documents is by writing the schema that they conform to, which makes the whole thing pointless.如果文档集是无限的,那么定义文档集的唯一真正方法是编写它们符合的模式,这会使整个事情变得毫无意义。

There are some constraints that are expressible in RNG but not in XSD, and there are also some constraints that are expressible in XSD and not in RNG.有一些约束可以在 RNG 中表达但不能在 XSD 中表达,也有一些约束可以在 XSD 中表达而不能在 RNG 中表达。

For example, RelaxNG can define more precise rules on the content of text nodes in mixed content, while XSD can define more precise rules on the cardinality range permitted for child elements.例如,RelaxNG 可以对混合内容中文本节点的内容定义更精确的规则,而 XSD 可以对子元素允许的基数范围定义更精确的规则。

The detailed comparison depends on which version of XSD you are talking about.详细比较取决于您所谈论的 XSD 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM