enrm
enrm

Reputation: 705

XSD/XML - Defining elements inside elements inside [...]

I wan't to be able to verify the following (XSD yes, I know) structure:

    <Mappings>
          <Mapping name="foo" type="bar">a string</Mapping>
          <Mapping name="Model" type="TableMapping" valueformat=">
             <Entry expression="some expression" value="some value"/>
             <Entry expression="some other expression" value="some other value"/>
          </Mapping>
    </Mappings>

To this end I have this schema:

<xs:complexType name="Mappings">
    <xs:sequence>
        <xs:element name="Mapping" maxOccurs="unbounded" minOccurs="1">
            <xs:complexType>
                <xs:simpleContent>
                    <xs:extension base="xs:string">
                        <xs:attribute type="xs:string" name="name"/>
                        <xs:attribute type="AllowedMappingTypes" name="type"/>
                        <xs:attribute type="xs:string" name="valueformat"/>
                    </xs:extension>
                </xs:simpleContent>

                <xs:element name="Entry" maxOccurs="unbounded" minOccurs="1">
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:sequence>
                                <xs:attribute type="xs:string" name="expression"/>
                                <xs:attribute type="xs:string" name="value"/>
                            </xs:sequence>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:element>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

I.e. the Mapping elements COULD have Entry elements in them, but this is not necessary. validation fails with formatting errors :

XMLSchemaParseError: Element'{http://www.w3.org/2001/XMLSchema}complexType': The content is not valid. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))., line 121

Which is this line:

 <xs:element name="Entry" maxOccurs="unbounded" minOccurs="1">

So my question is; how do I correctly write the XSD to be able to have a Mapping with a list of attributes and an optional Entry which has some specific attributes? I believe this is probably something easy but I'm lacking.. Tried reorganizing tags and trying different ones but it didn't work. Regards

Upvotes: 0

Views: 510

Answers (1)

enrm
enrm

Reputation: 705

The following code seems to handle my problem fine. One should always remember to run the code through an XSD generator! Apparently you need a <complexType mixed="true"> in the markup to allow elements and attributes inside an element.

<xs:complexType name="Mappings">
    <xs:sequence>
        <xs:element name="Mapping" maxOccurs="unbounded" minOccurs="0">
            <xs:complexType mixed="true">
                <xs:sequence>
                    <xs:element name="Entry" maxOccurs="unbounded" minOccurs="0">
                        <xs:complexType>
                            <xs:simpleContent>
                                <xs:extension base="xs:string">
                                    <xs:attribute type="xs:string" name="expression" use="optional"/>
                                    <xs:attribute type="xs:string" name="value" use="optional"/>
                                </xs:extension>
                            </xs:simpleContent>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute type="xs:string" name="name" use="optional"/>
                <xs:attribute type="AllowedMappingTypes" name="type" use="optional"/>
                <xs:attribute type="xs:string" name="valueformat" use="optional"/>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

Upvotes: 1

Related Questions