繁体   English   中英

没有顺序和多个发生的XML模式

[英]XML schema for no sequence and multiple occurs

  1. 我需要创建一个架构来验证XML文件。
  2. Mother标签内的元素可以是SonDaughter
  3. Son元素或Daughter元素没有任何顺序。
  4. Son不能兼任我(只有一个儿子)。
  5. 但是, Daughter可以是多个(多个女儿)。

所以我的问题是如何为此编写XML模式。 这就是我写的 我尝试使用<xsd:sequence><xsd:choice>代替<xsd:all> 但是我不知道该如何克服。

<xsd:complexType name="Mother">     
 <xsd:all>
  <xsd:element name="Son" type="string" minOccurs="0" maxOccurs="1"/>
  <xsd:element name="Daughter" type="string" minOccurs="0" maxOccurs="1"/>
 </xsd:all>
</xsd:complexType>

-------------------------------这些是正确的XML文件----------------- -------------------

有多个女儿

<Mother>
<Son>Jhon</Son>
<Daughter>Rose</Daughter>
<Daughter>Ann</Daughter>
</Mother>

顺序不同

<Mother>
<Daughter>Rose</Daughter>
<Son>Jhon</Son>
<Daughter>Ann</Daughter>
</Mother>

没有儿子或没有女儿或没有两者

<Mother>
<Daughter>Rose</Daughter>
</Mother>

在这种特殊情况下,你可以做到这一点作为一个sequence的零个或更多的Daughter元素,其次是零或一个Son ,然后又零个或多个Daughter小号

<xsd:complexType name="Mother">     
 <xsd:sequence>
  <xsd:element name="Daughter" type="string" minOccurs="0" maxOccurs="unbounded"/>
  <xsd:element name="Son" type="string" minOccurs="0" maxOccurs="1"/>
  <xsd:element name="Daughter" type="string" minOccurs="0" maxOccurs="unbounded"/>
 </xsd:sequence>
</xsd:complexType>

如果元素的内容不仅仅是一个字符串,那么我倾向于声明单独的顶级元素,并使用<xsd:element ref="Daughter" minOccurs=....来引用它们。

暂无
暂无

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

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