Search code examples
xmlxsd

Add multiple elements of same name and type to XSD1.0


Here is my xml -

<?xml version="1.0" encoding="utf-8" ?>
<choices xmlns="http://tempuri.org/XMLSchema2.xsd">
    <choice id="c1" text="text 1" description="description1" isCommentRequired="true"></choice>
    <choice id="c2" text="text 2" description="description2" isCommentRequired="false"></choice>
</choices>

I would like to develop schema for the same and my attempt is as follows -

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema2" targetNamespace="http://tempuri.org/XMLSchema2.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema2.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:element name="choices">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="choice" type="choiceType">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:boolean">
                                <xs:attribute name="id" type="xs:string" fixed="c1"/>
                                <xs:attribute name="text" type="xs:string" fixed="text 1"/>
                                <xs:attribute name="isCommentRequired" type="xs:boolean" fixed="true"/>
                                <xs:attribute name="description" type="xs:string" fixed="description1"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="choice" type="choiceType">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:boolean">
                                <xs:attribute name="id" type="xs:string" fixed="c2"/>
                                <xs:attribute name="text" type="xs:string" fixed="text 2"/>
                                <xs:attribute name="isCommentRequired" type="xs:boolean" fixed="false"/>
                                <xs:attribute name="description" type="xs:string" fixed="description2"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="choiceType">
        <xs:simpleContent>
            <xs:extension base="xs:boolean">
                <xs:attribute name="id" type="xs:string"/>
                <xs:attribute name="text" type="xs:string"/>
                <xs:attribute name="isCommentRequired" type="xs:boolean"/>
                <xs:attribute name="nextQuestionId" type="xs:string"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>

I am getting warning The type attribute cannot be present with either simpleType or complexType.

Constraints -

  1. xsd1.0
  2. I would like choice elements to have these fixed attributes in schema and in xml and not be changed by the user.

Does anyone have any idea how to correct the schema please?


Solution

  • There are several things wrong here.

    Firstly, the thing it's actually complaining about is

    <xs:element name="choice" type="choiceType">
       <xs:complexType>
    

    You can define the type of an element either by reference to a named global type, or with a local inline <complexType> declaration, but it makes no sense to do both.

    Secondly, there is no sense in repeating the element declaration with name="choice". If it will always occur exactly twice, write

    <xs:element name="choice" minOccurs="2" maxOccurs="2">
    

    Thirdly, you've defined the type as an extension of xs:boolean (extended with attributes), but in your instance document the <choice> elements are empty (they only have attributes, no content).