Search code examples
xmlreferencexsdcomplextype

XSD reference a type defined within the same file


I an trying to create an XSD, to define complex types to be referenced in other XSDs. So far so good. Within this XSD (playlist.xsd) I have one main complexType "playlistType". I want the playlistType to be able to contain multiple instances of an element of type 'secondaryEventType', which I am trying to define as a separate complexType within the same XSD file.

However, when I come to try to create the 'secondaryEvent' element within the 'playlistType' I am continuously getting the error message : "'secondaryEventType' must refer to an existing simple or complex type.". I have tried placing the definition of the 'secondaryEventType' both before and after the definition of the main 'playlistType', I have tried with and without the xs: prefix.

What am I doing wrong?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsB="baseTypes" targetNamespace="playlist">
    <xs:import namespace="baseTypes" schemaLocation="baseTypes.xsd"/>

    <xs:complexType name="secondaryEventType">
        <xs:sequence>
            <xs:element name="type">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="graphic"/>
                        <xs:enumeration value="bug"/>
                        <xs:enumeration value="voiceover"/>
                        <xs:enumeration value="subtitle"/>
                        <xs:enumeration value="addInsertionAvail"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="playlistType">
        <xs:sequence>
            <xs:element name="primaryEvent" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="eventID" type="xsB:guidType"/>
                        <xs:element name="eventTitle" type="xs:string"/>
                        <xs:element name="mediaID">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="2"/>
                                    <xs:pattern value="\S+"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="SOM" type="xsB:timecodeType"/>
                        <xs:element name="DUR" type="xsB:timecodeType"/>
                        <xs:element name="transition" minOccurs="0">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="cut"/>
                                    <xs:enumeration value="dissolve"/>
                                    <xs:enumeration value="fade"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="transitionDuration" type="xsB:timecodeType" minOccurs="0"/>
                        <xs:element name="secondaryEvent" type="secondaryEventType" minOccurs="0" maxOccurs="unbounded"/>
<!-- Error occurs Here -->
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Solution

  • Reference the name of the playlistType complex type using the namespace given by the XSD's targetNamespace. Your updated, working XSD follows...

    Stub baseTypes.xsd:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:xsB="baseTypes"
               targetNamespace="baseTypes">
      <xs:complexType name="guidType"/>
      <xs:complexType name="timecodeType"/>
    </xs:schema>
    

    Working main XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:xsB="baseTypes"
               xmlns:pl="playlist"
               targetNamespace="playlist">
      <xs:import namespace="baseTypes" schemaLocation="baseTypes.xsd"/>
      <xs:complexType name="secondaryEventType">
        <xs:sequence>
          <xs:element name="type">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:enumeration value="graphic"/>
                <xs:enumeration value="bug"/>
                <xs:enumeration value="voiceover"/>
                <xs:enumeration value="subtitle"/>
                <xs:enumeration value="addInsertionAvail"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="playlistType">
        <xs:sequence>
          <xs:element name="primaryEvent"
                      minOccurs="1"
                      maxOccurs="unbounded">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="eventID" type="xsB:guidType"/>
                <xs:element name="eventTitle" type="xs:string"/>
                <xs:element name="mediaID">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:minLength value="2"/>
                      <xs:pattern value="\S+"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="SOM" type="xsB:timecodeType"/>
                <xs:element name="DUR" type="xsB:timecodeType"/>
                <xs:element name="transition" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:enumeration value="cut"/>
                      <xs:enumeration value="dissolve"/>
                      <xs:enumeration value="fade"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="transitionDuration"
                            type="xsB:timecodeType"
                            minOccurs="0"/>
                <xs:element name="secondaryEvent"
                            type="pl:secondaryEventType"
                            minOccurs="0"
                            maxOccurs="unbounded"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>