Search code examples
xmlxsdxsd-validation

XSD convert JSON array


I'm trying to convert below JSON to XSD, however I was not able to come up with the correct XSD for the array type.

{
    "activeIndicator": true,
    "entities": [
        {
            "type": "one",
            "bid": "12444",
            "name": "dsdsddd"
        },
        {
            "type": "sss",
            "bid": "322333",
            "name": "sdfsfff"
        },
        {
            "type": "sddssddsd",
            "bid": "4343434",
            "name": "ffdssdddd"
        },
        {
            "type": "rerererer",
            "bid": "5767767",
            "name": "fdsfdffff"
        }
    ],
    "expiryDateIndicator": true
}

Below is the XSD I have come up with so far.

<xsd:complexType name="EntityType">
    <xsd:sequence>
        <xsd:element name="type" type="xsd:string"/>
        <xsd:element name="bid" type="xsd:string"/>
        <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:element name="Response">
    <xs:complexType>
        <xsd:sequence>
            <xsd:element name="activeIndicator" type="xsd:boolean" minOccurs="0" maxOccurs="1"/>
            <xs:element name="entities">
                <xs:simpleType>
                    <xs:list itemType="EntityType"/>
                </xs:simpleType>
            </xs:element>
        </xsd:sequence>
    </xs:complexType>
</xsd:element>

But above is throwing an org.xml.sax.SAXParseException: undefined simple type 'EntityType' when try to compile. curious to know what I'm missing here.


Solution

  • The item type of a list must be a simple type, not a complex type.

    You haven't shown the XML instance that you want to use to represent this JSON data, but I think it's unlikely to make use of list types. I would expect something like

    <entities>
      <entity>
        <type>x</type>
        <bid>y</bid>
        <name>z</name>
      </entity>
      <entity>
        <type>x</type>
        <bid>y</bid>
        <name>z</name>
      </entity>
    </entities>
    

    in which case entities would be defined as:

        <xs:element name="entities">
            <xs:complexType>
                <xs:sequence maxOccurs="unbounded">
                   <xs:element name="entity" type="EntityType"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>