2

I am trying to build a C# class from a XSD file. In short I'm getting a circular reference error when trying to execute the XSD.exe tool:

Group 'setFilterCondition' from targetNamespace='http://developer.cognos.com /schemas/report/7.0/' has invalid definition: Circular group reference.

I pulled the definition of the group and I have the following XSD for it:

<xs:group name="setFilterCondition">
        <xs:annotation>
            <xs:documentation>Represents a simple or compound set filter condition</xs:documentation>
        </xs:annotation>
        <xs:choice>
            <xs:element name="setFilterAnd">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                </xs:annotation>
                <xs:complexType>
                    <xs:sequence>
                        <xs:group ref="setFilterCondition" minOccurs="2" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="setFilterOr">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                </xs:annotation>
                <xs:complexType>
                    <xs:sequence>
                        <xs:group ref="setFilterCondition" minOccurs="2" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="setFilterNot">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                </xs:annotation>
                <xs:complexType>
                    <xs:group ref="setFilterCondition"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="memberCaptionCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a condition against the member caption.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="contains">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_contains"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_beginsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_endsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matches">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_matches"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="containsNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_containsNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_beginsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_endsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matchesNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_matchesNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="stringPropertyCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a condition against a string member property. The value attribute must be provided for all operators except isNull and isNotNull.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:all>
                        <xs:element ref="dmMemberProperty"/>
                    </xs:all>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="contains">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_contains"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_beginsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_endsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matches">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_matches"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="containsNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_containsNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_beginsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_endsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matchesNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_matchesNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="isNull"/>
                                <xs:enumeration value="isNotNull"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:string" use="optional"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="numericPropertyCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a numeric condition. The condition can be for either a tuple or a member property. The value attribute must be provided for all operators except isNull and isNotNull.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:all>
                        <xs:element ref="dmMemberProperty"/>
                    </xs:all>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="equal"/>
                                <xs:enumeration value="notEqual"/>
                                <xs:enumeration value="greaterThan"/>
                                <xs:enumeration value="greaterThanEqual"/>
                                <xs:enumeration value="lessThan"/>
                                <xs:enumeration value="lessThanEqual"/>
                                <xs:enumeration value="isNull"/>
                                <xs:enumeration value="isNotNull"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:decimal" use="optional"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="tupleCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a numeric condition. The condition can be for either a tuple or a member property. The value attribute must be provided for all operators except isNull and isNotNull.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:all>
                        <xs:element ref="dmTuple"/>
                    </xs:all>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="equal"/>
                                <xs:enumeration value="notEqual"/>
                                <xs:enumeration value="greaterThan"/>
                                <xs:enumeration value="greaterThanEqual"/>
                                <xs:enumeration value="lessThan"/>
                                <xs:enumeration value="lessThanEqual"/>
                                <xs:enumeration value="isNull"/>
                                <xs:enumeration value="isNotNull"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:decimal" use="optional"/>
                </xs:complexType>
            </xs:element>
        </xs:choice>
</xs:group>

I see that it has sub groups that are of the same type of the parent. Why does this cause a circular reference? How can I edit this XSD to not cause the error?

Mike G
  • 1,956
  • 1
  • 30
  • 62

1 Answers1

4

The same was asked on SO a while ago... this is the post, with some answers.

The solution I am proposing should be simpler, especially in your case where you can edit the XSD and the fact is the group itself fully describes the content of the element where is used.

The trick is then to reuse and implement the recusivity of your content not through group reference, but through complex type. If you compare your schema to the mine below, you'll notice how I've defined and used the complex type.

Hopefully it'll help someone else....

The refactored XSD works with XSD.exe, the class generates clean, without any error.

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xs:schema xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:annotation>
        <xs:documentation>Generated from "Set2" under "Release1"</xs:documentation>
    </xs:annotation>
    <xs:element name="dmTuple" type="xs:string"/>
    <xs:element name="dmMemberProperty" type="xs:string"/>
    <xs:complexType name="setFilterConditionList">
        <xs:sequence minOccurs="2" maxOccurs="unbounded">
            <xs:group ref="setFilterCondition"/>
        </xs:sequence>      
    </xs:complexType>
    <xs:group name="setFilterCondition">
        <xs:choice>
            <xs:element ref="setFilterAnd">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                </xs:annotation>
            </xs:element>
            <xs:element ref="setFilterOr">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                </xs:annotation>
            </xs:element>
            <xs:element ref="setFilterNot">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                </xs:annotation>
            </xs:element>
            <xs:element name="memberCaptionCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a condition against the member caption.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="contains">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_contains"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_beginsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_endsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matches">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_matches"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="containsNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_containsNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_beginsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_endsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matchesNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_memberCaptionCondition_operator_matchesNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="stringPropertyCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a condition against a string member property. The value attribute must be provided for all operators except isNull and isNotNull.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:all>
                        <xs:element ref="dmMemberProperty"/>
                    </xs:all>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="contains">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_contains"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_beginsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWith">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_endsWith"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matches">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_matches"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="containsNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_containsNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="beginsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_beginsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="endsWithNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_endsWithNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="matchesNot">
                                    <xs:annotation>
                                        <xs:documentation source="doc_enum_stringPropertyCondition_operator_matchesNot"/>
                                    </xs:annotation>
                                </xs:enumeration>
                                <xs:enumeration value="isNull"/>
                                <xs:enumeration value="isNotNull"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:string" use="optional"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="numericPropertyCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a numeric condition. The condition can be for either a tuple or a member property. The value attribute must be provided for all operators except isNull and isNotNull.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:all>
                        <xs:element ref="dmMemberProperty"/>
                    </xs:all>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="equal"/>
                                <xs:enumeration value="notEqual"/>
                                <xs:enumeration value="greaterThan"/>
                                <xs:enumeration value="greaterThanEqual"/>
                                <xs:enumeration value="lessThan"/>
                                <xs:enumeration value="lessThanEqual"/>
                                <xs:enumeration value="isNull"/>
                                <xs:enumeration value="isNotNull"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:decimal" use="optional"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="tupleCondition">
                <xs:annotation>
                    <xs:documentation source="rn_added_7.0"/>
                    <xs:documentation>Defines a numeric condition. The condition can be for either a tuple or a member property. The value attribute must be provided for all operators except isNull and isNotNull.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:all>
                        <xs:element ref="dmTuple"/>
                    </xs:all>
                    <xs:attribute name="operator" use="required">
                        <xs:annotation>
                            <xs:documentation source="doc_att_filterCondition_operator"/>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="equal"/>
                                <xs:enumeration value="notEqual"/>
                                <xs:enumeration value="greaterThan"/>
                                <xs:enumeration value="greaterThanEqual"/>
                                <xs:enumeration value="lessThan"/>
                                <xs:enumeration value="lessThanEqual"/>
                                <xs:enumeration value="isNull"/>
                                <xs:enumeration value="isNotNull"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="value" type="xs:decimal" use="optional"/>
                </xs:complexType>
            </xs:element>
        </xs:choice>
    </xs:group>
    <xs:element name="setFilterAnd" type="setFilterConditionList"/>
    <xs:element name="setFilterOr" type="setFilterConditionList"/>
    <xs:element name="setFilterNot">
        <xs:complexType>
            <xs:group ref="setFilterCondition"/>
        </xs:complexType>
    </xs:element>   
</xs:schema>
Community
  • 1
  • 1
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • This refactoring does not seem to be equivalent. Where has gone the `minOccurs="2" maxOccurs="unbounded"` constraints? – Tim Aug 27 '15 at 11:51
  • @Tim, thank you for catching the error; I've updated it and tested it, all is good now. – Petru Gardea Aug 27 '15 at 16:16
  • Thank you for your time. Unfortunately Groups are back in you edit. The aim of this answer was to replace them by ComplexTypes. Wasn't it? – Tim Aug 28 '15 at 13:48
  • 1
    @Tim, I left that in to keep it compact; groups are no longer required to achieve recursive-ness, instead the complex types/elements do it. So I achieved what I set out to; it is also why xsd.exe doesn't complain anymore. Another reason I didn't replace it here, was to avoid comments on "reuse"; keep in mind we don't do this refactoring by hand; so while the output is verbose, harder to read, and definitely impractical to maintain, it works with a tool that has limitations. We've done the same for an XHTML schema subset referenced by FHIR, etc. – Petru Gardea Aug 28 '15 at 14:46
  • It doesn't seem to work for me; I keep receiving the following message: "The 'http://www.w3.org/2001/XMLSchema:complexType' element is not supported in this context." – DennisVM-D2i Jun 01 '23 at 13:37
  • @DennisVM-D2i it is not clear what it is that is not working for you. I just rechecked the schema in this post, and it is valid. I suggest you create a new question, post your relevant bit, and hopefully someone will answer. – Petru Gardea Jun 02 '23 at 16:23
  • @PetruGardea Yes, my comment was general - relating to a different schema, and the C#/.NET 'XSD' too; but I thought it might possibly be applicable here, so thought best to at least mention the issue just in case. – DennisVM-D2i Jun 05 '23 at 06:55
  • @DennisVM-D2i I am pretty sure that your error is not related to this. This post is about an XSD that is valid, but no consumable by a tool (in this case xsd.exe). Your error message sounds more as an invalid schema file which must be corrected first. If you're still having issues, you may create a separate ticket. – Petru Gardea Jun 06 '23 at 15:05