Muhammad Hewedy
Muhammad Hewedy

Reputation: 30058

How to write an XSD for a list with no wrapper element

Is it possible to have xsd:complexType contains xsd:sequence and xsd:element?

<xsd:complexType name="employeesType" mixed="true">
    <xsd:sequence>
        <xsd:element name="employee" type="employeeType"
            maxOccurs="unbounded" minOccurs="0" />
    </xsd:sequence>
    <!-- ERROR
    <xsd:element name="responseTime" type="responseTimeType"></xsd:element>
     -->
</xsd:complexType>

Upvotes: 1

Views: 2104

Answers (1)

hielsnoppe
hielsnoppe

Reputation: 2869

As you show in your example above it is not allowed to have an <xsd:element /> as a direct child of <xsd:complexType /> (see reference at w3schools.com). Considering the example from your previous question I think what you want to do can be achieved with a schema like this:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="date" type="xsd:string" />
            <xsd:element name="responseTime" type="xsd:decimal" />
            <xsd:element name="employee" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="name" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
</xsd:schema>

This validates for your example code:

<?xml version="1.0"?>
<root>
    <date> 9:51 AM 10/10/2012 </date>
    <responseTime> 1.20</responseTime>
    <employee>
        <name> Mohammad</name>
    </employee>
    <employee>
        <name> Ali</name>
    </employee>
    <employee>
        <name> Mostafa</name>
    </employee>
    <employee>
        <name> Mahmoud</name>
    </employee>
</root>

You might want to change the type of <date /> to xsd:date though.

Upvotes: 1

Related Questions