加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML Schema(XSD) – 如何指定父元素包含至少一个子元素?

发布时间:2020-12-16 07:57:12 所属栏目:百科 来源:网络整理
导读:我有一个XML模式(XSD),它将元素定义为必需元素(称之为父元素);让我们说,这个父级有五个子元素,它们都可以是可选的,但必须至少有一个子元素. 我怎样才能在xsd中指定它? 澄清:孩子是不同的元素和可选的. 例如. Parent Child1contents are different to other
我有一个XML模式(XSD),它将元素定义为必需元素(称之为父元素);让我们说,这个父级有五个子元素,它们都可以是可选的,但必须至少有一个子元素.

我怎样才能在xsd中指定它?

澄清:孩子是不同的元素和可选的.
例如.

<Parent>
   <Child1>contents are different to other siblings and arbitrary</Child1>
   <Child2>can be text,a simple element,or another complex element</Child2>
   <Child3>etc.. etc</Child3> 
</Parent>

<xs:complexType name="Parent">
  <xs:sequence>
    <xs:element minOccurs="0" name="Child1" type="xs:string"/>
    <xs:element minOccurs="0" name="Child2" />
    <xs:element minOccurs="0" name="Child3" />
  </xs:sequence>
</xs:complexType>

即使每个孩子都是可选的,父母也需要至少有一个孩子.

始终有直接的方法:
<xs:complexType name="Parent">
  <xs:choice>
    <xs:sequence>
      <xs:element name="Child1"/>
      <xs:element name="Child2" minOccurs="0"/>
      <xs:element name="Child3" minOccurs="0"/>
    </xs:sequence>
    <xs:sequence>
      <xs:element name="Child2"/>
      <xs:element name="Child3" minOccurs="0"/>
    </xs:sequence>
    <xs:sequence>
      <xs:element name="Child3"/>
    </xs:sequence>
  </xs:choice>
</xs:complexType>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读