问题描述
我想要做的是,声明一个名为data"的父元素,这两个元素的 6 个子元素是有条件的,这意味着如果选择元素 A,则 B 不会出现在data"中.
What I'm trying to do is, declare an parent element called "data", which are having 6 sub element of these two element are conditional that means if element A is choose then B is not appear in "data".
像这样:
<data>
<A>text1</A>
<B>text1</B>
<C>text1</C>
<D>text1</D>
<E>text1</E> or <F>text1</F>
</data>
要求 1:所有元素都可以以任意顺序出现任意次数.
Requirement 1 : all element can appear in any order and any number of times.
要求 2: 元素 E &F 是有条件的意味着只有一个 then 出现在数据中.
Requirement 2 : Element E & F are conditional means only one of then is apear in data.
我的 xsd 代码是这样的:
My xsd code is this:
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="sequence" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded" >
<xs:element ref="A" />
<xs:element ref="B" />
<xs:element ref="C" />
<xs:element ref="D" />
<xs:choice>
<xs:element ref="E" />
<xs:element ref="F" />
<xs:element ref="G" />
</xs:choice>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="status"/>
</xs:complexType>
</xs:element>
我已经尝试了所有这些链接,但仍然没有得到我的解决方案.
I have tried all these link but still not getting my solution.
推荐答案
您与内容模型的约束发生冲突,这些约束被称为歧义规则"(在 SGML 中)、确定性规则"(在 XML 中称为,拒绝将它们称为歧义规则,因为实际上被禁止的实际上并不是歧义,而是从内容模型以特定方式形成的有限状态自动机的非确定性),或独特的粒子属性规则"(所谓的因为 XSD 1.0 的主编似乎认为它们在某些方面与 XML 的确定性规则不同;我已经忘记了应该有什么区别,只记得它是基于对 XML 规则的误解).它们实际上并没有很好的技术动机(它们是 SGML 中的一个设计错误,出于兼容性原因被转移到 XML 中,并且完全没有理由被转移到 XSD 中),并且(如您所见)它们使合法的构建复杂化内容模型,这是一些人对 Relax NG 如此满意的原因之一,因为它基本上消除了它们.但是,无论好坏,它们都是 XML 文档语法的众多语言的一部分,因此了解如何遵守它们是值得的.
Your are running afoul of a constraint on content models known variously as the "ambiguity rules" (in SGML), the "determinism rules" (so called in XML, which declined to call them ambiguity rules because in fact what is forbidden is not actually ambiguity, but non-determinism of the finite state automaton formed in a particular way from the content model), or the "unique particle attribution rules" (so called because the lead editor of XSD 1.0 appears to have thought they were different in some way from the determinism rules of XML; I've forgotten what the difference was supposed to be, and only remember that it was based on a misunderstanding of the XML rules). They don't actually have a good technical motivation (they were a design error in SGML, carried forward into XML for compatibility reasons and into XSD for no good reason at all), and (as you have found) they complicate the construction of legal content models, which is one reason some people are so happy with Relax NG, which mostly eliminates them. But since for better or worse they are part of so many languages for XML document grammars, it is worth knowing how to comply with them.
您描述的语言是:A、B、C 或 D 元素的任何序列,与 E 元素或 F 元素混合.要在确定性内容模型中描述这种语言,您可能会发现考虑自动机必须具有哪些不同状态才能识别这种语言会很有帮助.在一种状态下,元素 A 到 F 中的任何一个都是合法的.元素 A 到 D 使我们处于那种状态.但是一旦我们看到 E 或 F,我们就会进入不同的状态,其中 A 到 D 以及 E 或 F(我们先看到的那个)是合法的,但另一个(F 或 E)不被接受.所以我们需要三个状态.
The language you describe is: any sequence of A, B, C, or D elements, intermixed either with E elements or with F elements. To describe this language in a deterministic content model, you may find it helpful to think about what different states an automaton would have to have, in order to recognize such a language. In one state, any of the elements A to F is legal. Elements A through D leave us in that state. But once we see an E or an F, we move into a different state, in which A through D and either E or F (whichever we saw first) is legal, but the other (F or E) is not accepted. So we need three states.
请注意,每个状态对应于它自己的一种语言:我们可以看到将我们置于该状态并让我们处于该状态的元素序列.如果我们将状态命名为 initial
、eee
和 eff
,我们可以用一个很容易翻译成 XSD 的正则表达式来概括这些语言:
Note that each state corresponds to a language of its own: the sequence of elements we can see that put us into that state and leave us in that state. If we name the states initial
, eee
, and eff
, we can summarize the languages with a regular expression which is easily translated into XSD:
- L(初始) = (A|B|C|D)*
- L(eee) = E, (A|B|C|D|E)*
- L(eff) = F, (A|B|C|D|F)*
请注意,从第一个状态,我们可以移动到第二个或第三个状态,但是一旦我们处于这两个状态中的任何一个状态,我们就永远不会离开它.这意味着我们想要的语言实际上是 L(initial) 后跟 L(eee) 或 L(eff) 的选择.所以一种 XSD 公式是:
Note that from the first state, we can move to the second or the third, but once we are in either of those states, we never leave it. This means that the language we want is effectively L(initial) followed by a choice of L(eee) or L(eff). So one XSD formulation would be:
<xsd:group name="initial">
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="A"/>
<xsd:element ref="B"/>
<xsd:element ref="C"/>
<xsd:element ref="D"/>
</xsd:choice>
</xsd:sequence>
</xsd:group>
<xsd:group name="eee">
<xsd:sequence>
<xsd:sequence minOccurs="0">
<xsd:element ref="E"/>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="A"/>
<xsd:element ref="B"/>
<xsd:element ref="C"/>
<xsd:element ref="D"/>
<xsd:element ref="E"/>
</xsd:choice>
</xsd:sequence>
</xsd:sequence>
</xsd:group>
<xsd:group name="eff">
<xsd:sequence minOccurs="0">
<xsd:element ref="F"/>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="A"/>
<xsd:element ref="B"/>
<xsd:element ref="C"/>
<xsd:element ref="D"/>
<xsd:element ref="F"/>
</xsd:choice>
</xsd:sequence>
</xsd:group>
<xsd:complexType name="data">
<xsd:sequence>
<xsd:group ref="initial"/>
<xsd:choice>
<xsd:group ref="eee"/>
<xsd:group ref="eff"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="data" type="data"/>
这相当于以不同的方式描述您的语言:A、B、C 或 D 元素的序列,后跟可选的 E,然后是任何 A、B、C、D 或 E 元素序列,或由 F 和任何 A、B、C、D 或 F 元素序列组成.值得花费尽可能多的时间来说服自己,所描述的语言与您在问题描述中所描述的语言相同.
This amounts to describing your language in a different way: a sequence of A, B, C, or D elements, followed optionally either by an E and then any sequence of A, B, C, D, or E elements, or by an F and then any sequence of A, B, C, D, or F elements. It is worth spending as much time as it takes to persuade yourself that the language so described is the same as the language you describe in your description of the problem.
这篇关于XML Schema 中的嵌套选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!