XSD - 对订单没有限制的必需,可选和*元素

问题描述:

我正在寻找一个XML报表的模式,它对元素的显示顺序没有任何限制,但有一个元素必须出现一次,可以出现一次的元素以及可以出现任意次数的元素。XSD - 对订单没有限制的必需,可选和*元素

我在this question的第二个答案之后创建了一个XSD,因为该解决方案(尽管很丑)应该可以解决我的问题。但是,使用微软的xsd.exe tool生成的XSD类或使用XmlDocument.Validate()了以下警告:

架构验证警告:元素“optional2”的多个定义会导致内容模型变得暧昧。内容模型必须形成以使...

错误是在第21行,这是第二次'optional2'出现在xsd中。

以下是我对我的the question I referenced earlier第二个答案的版本。

<xs:group name="unboundedElements"> 
    <xs:choice> 
     <xs:element name="unbounded1" type="unbounded1Type"/> 
     <xs:element name="unbounded2" type="unbounded2Type"/> 
     <xs:element name="unbounded3" type="unbounded3Type"/> 
     <xs:element name="unbounded4" type="unbounded4Type"/> 
    </xs:choice> 
</xs:group> 
<xs:element name="root"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
      <xs:choice> 
       <xs:sequence> 
        <xs:element name="optional1" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional2" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional3" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional4" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="required" type="queryType" maxOccurs="1"/> 
       </xs:sequence> 
       <xs:sequence> 
        <xs:element name="optional2" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional1" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional3" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional4" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="required" type="queryType" maxOccurs="1"/> 
       </xs:sequence> 
       <xs:sequence> 
        <xs:element name="optional3" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional2" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional1" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="optional4" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        <xs:group ref="unboundedElements" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="required" type="queryType" maxOccurs="1"/> 
// And so on, since this is 5! = 120 permutations 
       </xs:sequence> 
      </xs:choice> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

我希望有人能指出并解释我做错了什么。

我可以通过在所有元素周围使用<xs:choice maxOccurs="unbounded">标签来“验证”xml,因为这样可以使命令无关紧要,但它不会在xml文档上放置任何必要的限制。

此外,我知道xsd 1.1允许使用maxOccurs="unbounded"<xs:all>这将完美解决问题,但.NET不支持使用xsd 1.1。

我在想我最好的解决方案可能只是通过在验证它之前对xml节点进行排序来强制执行严格的元素排序(允许xsd文档更加简单和正确),但我仍然希望了解有关xsd的更多信息,并尝试弄清楚我的尝试有什么问题。

如果您希望在特定部分(不是多次和未排序)中只有一次节点“optional1”...“optional4”,那么您必须定义所有可能的命令,使第一个节点成为必需的摆脱歧义。您可能需要多次遵循此方法。希望这会有所帮助,彼得

+0

在我的xsd中,我确实拥有了可选项1到可选项4以及所需元素(所以总共5个元素,每个排列都是120个可能的顺序)的所有可能的顺序。每个置换都在一个序列标签内,每个序列标签都在一个选择标签内。 通过使第一个节点成为强制性以消除歧义性,你的意思是什么? –

+0

当根据xml模式检查xml文档时,任何程序都必须遵循一种决策树(这意味着遵循xml模式的路径)。在树的每一个点上,只有一种可能性来跟随这棵树,所以任何时候都不允许含糊不清。使用强制节点可以帮助您避免这些情况。 –

+0

为了更加精确,我需要有关应用程序中可能的组合的更多信息,例如A-B-C-D-E和A-C-B-D-E是有效的,但是A-E-D-C-B无效。 –