Search code examples
xmlxsdxsd2code

xsd - circular dependencies between complex types


I am looking at some of the xsd files available here: https://www.isotc211.org/2005/gmd/

I see this in https://www.isotc211.org/2005/gmd/citation.xsd

<xs:complexType name="CI_Citation_Type">
    <xs:complexContent>
        <xs:extension base="gco:AbstractObject_Type">
            <xs:sequence>
                ...
                <xs:element name="identifier" type="gmd:MD_Identifier_PropertyType" minOccurs="0" maxOccurs="unbounded"/>
                ...
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:element name="CI_Citation" type="gmd:CI_Citation_Type"/>

<xs:complexType name="CI_Citation_PropertyType">
    <xs:sequence minOccurs="0">
        <xs:element ref="gmd:CI_Citation"/>
    </xs:sequence>
    ...
</xs:complexType>

Which means

  • CI_Citation_PropertyType contains seq terms of type CI_Citation_Type
  • and CI_Citation_Type contains seq terms of type MD_Identifier_PropertyType

On the other hand, I see this in https://www.isotc211.org/2005/gmd/referenceSystem.xsd

<xs:complexType name="MD_Identifier_Type">
    <xs:complexContent>
        <xs:extension base="gco:AbstractObject_Type">
            <xs:sequence>
                <xs:element name="authority" type="gmd:CI_Citation_PropertyType" minOccurs="0"/>
                ...
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:element name="MD_Identifier" type="gmd:MD_Identifier_Type"/>

<xs:complexType name="MD_Identifier_PropertyType">
    <xs:sequence minOccurs="0">
        <xs:element ref="gmd:MD_Identifier"/>
    </xs:sequence>
    <xs:attributeGroup ref="gco:ObjectReference"/>
    <xs:attribute ref="gco:nilReason"/>
</xs:complexType> 

Which means

  • MD_Identifier_PropertyType contains seq terms of type MD_Identifier_Type
  • and MD_Identifier_Type contains seq terms of type CI_Citation_PropertyType

So

This looks like a circular dependency between xsd types CI_Citation_PropertyType and MD_Identifier_PropertyType.

My questions:

  1. Is this valid/legal xsd? Are such "circular dependencies" a concern in xsd/xml? (I think circular references are accepted in xsd schemas)

  2. I try to generate mapping classes for these types. I can't figure it out to deal with such dependencies (in c++, where #includes are sequential. Maybe via forward declarations and pointers...).

Thanks a lot


Solution

  • XSD obviously has to allow recursive structures for document models, e.g. where tables are nested within tables. So it should be no surprise that such structures are legal.

    I can't help you on how to generate mapping classes, but languages like C++ also allow recursive data structures.