【问题标题】:Help With Self Referencing XSD Producing Circular Reference帮助自引用 XSD 生成循环引用
【发布时间】:2011-10-30 18:10:51
【问题描述】:

我正在尝试从 XSD 文件构建 C# 类。简而言之,我在尝试执行 XSD.exe 工具时遇到循环引用错误:

组“setFilterCondition”来自 targetNamespace='http://developer.cognos.com/schemas/report/7.0/' 有 无效定义:循环组引用。

我提取了组的定义,我有以下 XSD:

<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>

我看到它具有与父级相同类型的子组。为什么这会导致循环引用?如何编辑此 XSD 以免导致错误?

【问题讨论】:

  • 迟到总比不...请看我的回复,实际上有一个非常简单的方法...

标签: xsd xsd.exe cognos


【解决方案1】:

不久前在 SO 上也提出了同样的问题...this is the post,并给出了一些答案。

我提出的解决方案应该更简单,尤其是在您可以编辑 XSD 并且该组本身完全描述了所使用元素的内容的情况下。

诀窍是重用和实现内容的递归性,而不是通过组引用,而是通过复杂类型。如果您将您的架构与下面的架构进行比较,您会注意到我是如何定义和使用复杂类型的。

希望它会帮助别人......

重构后的 XSD 与 XSD.exe 配合使用,类生成干净,没有任何错误。

<?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>

【讨论】:

  • 这种重构似乎并不等效。 minOccurs="2" maxOccurs="unbounded" 约束到哪里去了?
  • @Tim,感谢您发现错误;我已经更新并测试了它,现在一切都很好。
  • 感谢您的宝贵时间。不幸的是,组又回到了您的编辑中。这个答案的目的是用 ComplexTypes 替换它们。不是吗?
  • @Tim,我把它留在里面以保持紧凑;不再需要组来实现递归,而是复杂的类型/元素来实现。所以我实现了我的目标;这也是 xsd.exe 不再抱怨的原因。我没有在这里替换它的另一个原因是避免“重用” cmets;请记住,我们不会手动进行此重构;因此,虽然输出很冗长,更难阅读,而且维护起来肯定不切实际,但它与一个有局限性的工具一起工作。我们对 FHIR 等引用的 XHTML 模式子集做了同样的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 2012-11-06
相关资源
最近更新 更多