Janek Nowak
Janek Nowak

Reputation: 57

xml schema choice selecting one item option or the other with a common part

I have a question about xml schema.I don't really know how to use choice properly. It is my xml text.

<serials>
     <serial>
        <title></title>
        <country></country>
        <director></director>
        <release date></release date>
     </serial>
     .
     .some of the same as the one above
     .
     <serial>
        <title></title>
        <country></country>
        <director></director>
        <uncertaine></uncertainee>
     </serial>
     <serial>
        <title></title>
        <country></country>
        <director></director>
        <scheduled time></scheduled time>
     </serial>
</serials>

and here's the problematic part of xml schema

<xsd:group name="serialData">
     <xsd:choice>
           <xsd:sequence>
                <xsd:element name="title" type="xsd"string"/>
                <xsd:element name="country" type="xsd"string"/>
                <xsd:element name="director" type="xsd"string"/>
                <xsd:element name="release date" type="xsd"string"/>
           </xsd:sequence>
           <xsd:sequence>
                <xsd:element name="title" type="xsd"string"/>
                <xsd:element name="country" type="xsd"string"/>
                <xsd:element name="director" type="xsd"string"/>
                <xsd:element name="scheduled time" type="xsd"string" minOccurs="0" maxOccurs="1"/>
                <xsd:element name="uncertaineetime" type="xsd"string" minOccurs="0" maxOccurs="1"/>
           </xsd:sequence>
     </xsd:choice>
</xsd:group>

the problem is that the latter atypical series want the same attributes as the former, but cannot have them.

Upvotes: 0

Views: 556

Answers (1)

Dijkgraaf
Dijkgraaf

Reputation: 11527

Let say your payload looked like this, which is Valid XML

<serials>
     <serial>
        <title>Title</title>
        <country>Country</country>
        <director>Director</director>
        <release_date>2021-12-06</release_date>
     </serial>
     <serial>
        <title>Title2</title>
        <country>Country</country>
        <director>Director</director>
        <uncertain_time>Uncertain</uncertain_time>
     </serial>
     <serial>
        <title>Title3</title>
        <country>Country</country>
        <director>Director</director>
        <scheduled_time>Scheduled</scheduled_time>
     </serial>
</serials>

You could simple have the schema as below where you make some of them optional.

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="serials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="serial">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" />
              <xs:element name="country" type="xs:string" />
              <xs:element name="director" type="xs:string" />
              <xs:element minOccurs="0" name="scheduled_time" type="xs:string" />
              <xs:element minOccurs="0" name="uncertain_time" type="xs:string" />
              <xs:element minOccurs="0" name="release_date" type="xs:date" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

If you mean that you can either have the Uncertain time or the schedule time or release date then those are the items you put in the Choice Node as per below

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="serials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="serial">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" />
              <xs:element name="country" type="xs:string" />
              <xs:element name="director" type="xs:string" />
              <xs:choice>
                <xs:element name="scheduled_time" type="xs:string" />
                <xs:element name="uncertain_time" type="xs:string" />
                <xs:element name="release_date" type="xs:date" />
              </xs:choice>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Upvotes: 1

Related Questions