简体   繁体   中英

XSD - element that contains one mandatory and one optional child element ( but both of them have to appear min once)

I have a element A which contains two child elements B and C , child elements have to appear in the specific order ( B first, C second ) and A contains at least one pair of B and C (where B is mandatory and C is optional). For instance, a valid example would be:

<A>
  <B></B>
  <C></C>
  <B></B>
  <B></B>
  <C></C>
</A>

I tried this,

<xsd:complexType name="A">
    <xsd:choice minOccurs="1" maxOccurs="unbounded">
        <xsd:sequence>
            <xsd:element name="B" type="BType" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="C" type="CType" minOccurs="1" maxOccurs="1"/>
        </xsd:sequence>
        <xsd:sequence>
            <xsd:element name="B" type="BType"/>
            <xsd:element name="C" type="CType" minOccurs="0" />
        </xsd:sequence>
    </xsd:choice>
</xsd:complexType>

but in my solution

<A><C></C></A>

would be valid (but I need at least one pair of B and C)

This should do the trick:

<xsd:complexType name="A">
    <xsd:sequence minOccurs="1" maxOccurs="unbounded">
        <xsd:element name="B" type="BType"/>
        <xsd:element name="C" type="CType" minOccurs="0" />
    </xsd:sequence>
</xsd:complexType>
<xsd:element name=“A”>
  <xsd:complexType name=“AType”>
    <xsd:choice maxOccurs=”unbounded” >
      <xsd:sequence>
        <xsd:element name=“B” type=“BType”/>
        <xsd:element name=“C” type=“CType”/>
      </xsd:sequence>
    <xsd:element name=“B” type=“BType”/>
  </xsd:choice>
</xsd:complexType>
</xsd:element>

Can this be possible solution?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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