Search code examples
xmlxsdxsd-validation

XSD Multiple elements with the same name but different attribute value


I'm trying to create an XML schema for the following:

        <SomeApp>
            <Org>Dataset</Org>
            <Name>Foo</Name>
            <Version>1.00</Version>
            <Source>25839</Source>
            <UniversalID>867-5309</UniversalID>
            <TestField Name="Price">7.00</TestField>
            <TestField Name="Level">5.00</TestField>
            <TestField Name="new">false</TestField>
            <TestField Name="Description">Bar</TestField>
        </SomeApp>

I'm OK until the parser reaches the <TestField> elements. I can handle one of these elements like so...

               <xs:element name="TestField" minOccurs="0">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:decimal">
                                <xs:attribute name="Name" type="xs:string" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>

...but of course the parser doesn't allow the next element of the same name, and defining it again (with the simpleContent set to match the next data type) doesn't do the trick either. Here is one of the XSD formats I've tried, just to pass the first two TestField elements:

                <xs:complexType name="TestFieldType">
                    <xs:simpleContent>
                        <xs:extension base="xs:decimal">
                            <xs:attribute name="Name" type="xs:string" use="required"/>
                                <xs:simpleType>
                                    <xs:extension base="xs:string">
                                        <xs:enumeration value="Price"/>
                                        <xs:enumeration value="Level"/>
                                    </xs:extension>
                                </xs:simpleType>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>

                <xs:element name="TestField" type="TestFieldType"/>

I would be grateful for any help!


Solution

  • Check it out.

    XML

    <SomeApp>
        <Org>Dataset</Org>
        <Name>Foo</Name>
        <Version>1.00</Version>
        <Source>25839</Source>
        <UniversalID>867-5309</UniversalID>
        <TestField Name="Price">7.00</TestField>
        <TestField Name="Level">5.00</TestField>
        <TestField Name="Description">Bar</TestField>
    </SomeApp>
    

    XSD

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
        <xs:element name="SomeApp">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="Org"/>
                    <xs:element ref="Name"/>
                    <xs:element ref="Version"/>
                    <xs:element ref="Source"/>
                    <xs:element ref="UniversalID"/>
                    <xs:element maxOccurs="unbounded" ref="TestField"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="Org" type="xs:string"/>
        <xs:element name="Name" type="xs:string"/>
        <xs:element name="Version" type="xs:decimal"/>
        <xs:element name="Source" type="xs:integer"/>
        <xs:element name="UniversalID" type="xs:string"/>
        <xs:element name="TestField">
            <xs:complexType>
                <xs:simpleContent>
                    <xs:extension base="xs:string">
                        <xs:attribute name="Name" use="required" type="TestFieldType"/>
                    </xs:extension>
                </xs:simpleContent>
            </xs:complexType>
        </xs:element>
    
        <xs:simpleType name="TestFieldType">
            <xs:restriction base="xs:string">
                <xs:enumeration value="Price"/>
                <xs:enumeration value="Level"/>
                <xs:enumeration value="Description"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:schema>