XML Schema (XSD) - 如何指定父元素以包含至少一个子元素? [英] XML Schema (XSD) - How to specify parent element to contain at least one child element?

查看:41
本文介绍了XML Schema (XSD) - 如何指定父元素以包含至少一个子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 模式 (XSD),它将元素定义为强制元素(称其为父元素);比方说,这个父元素有五个子元素,它们都可以是可选的,但必须至少出现一个子元素.

I have an XML schema (XSD) that defines an element as mandatory (call it the parent); this parent has, lets say, five child elements, which can all be optional, BUT AT LEAST ONE child element must occur.

我如何在 xsd 中指定它?

How can i specify this in the xsd?

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

To clarify: The children are different elements and optional. For example.

<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>

尽管每个孩子都是可选的,但父母至少需要有一个孩子.

Even though every child is optional, the parent needs to have at least one child.

推荐答案

总是有直接的方法:

<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>

这篇关于XML Schema (XSD) - 如何指定父元素以包含至少一个子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆