Are circular groups allowed by XSD schema?

試著忘記壹切 提交于 2019-12-12 15:09:16

问题


For this xml:

<elem1 xmlns="http://www.fixprotocol.org/ns/fast/t/1.0">
 <elem2>
   <elem2/>
 </elem2>
</elem1>

I have this schema, which seems to validate fine against w3 schema validation service, and the schema validates the above XML just fine. Sadly, xsd.exe and some other tools report it to be an error. Is that correct? Are circular group refs dissallowed by XML schema? Thanks!

Update: The schema is not mine, can't change it :(

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:t="http://www.fixprotocol.org/ns/fast/t/1.0">

  <xs:element name="elem1">
    <xs:complexType>
      <xs:group ref="t:grp1" />
   </xs:complexType>
  </xs:element>

  <xs:group name="grp1">
    <xs:sequence>
      <xs:group ref="t:grp2" />
    </xs:sequence>
  </xs:group>

  <xs:group name="grp2">
    <xs:sequence>
      <xs:element minOccurs="0" name="elem2">
        <xs:complexType>
          <xs:group ref="t:grp1" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:group>

</xs:schema>

回答1:


This question is being linked with many recent questions that talk about the same problem: circular groups, and Microsoft's xsd.exe, hence I think it should be answered, even though it is quite "old".

The confusion is caused by what qualifies as a circular group. According to section 3.8.6 of the XSD spec:

"Circular groups are disallowed. That is, within the {particles} of a group there must not be at any depth a particle whose {term} is the group itself."

Based on the above, your example is not a circular group, since the group itself does not rely on itself as a particle. Your schema is valid.

This is a circular group:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="elem1">
        <xsd:complexType>
            <xsd:group ref="grp1"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:group name="grp1">
        <xsd:sequence>
            <xsd:choice>
                <xsd:group ref="grp1"/>
            </xsd:choice>                       
        </xsd:sequence>
    </xsd:group>
</xsd:schema>

One cannot rewrite a true circular group. However, your example can be rewritten in a couple of ways: the schema below shows an equivalent content model, based on recursive complex types.

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:annotation>
        <xsd:documentation xmlns="">Generated from "Set1" under "Release2"</xsd:documentation>
    </xsd:annotation>

    <xsd:complexType name="grp1">
        <xsd:sequence>
            <xsd:element minOccurs="0" name="elem2" type="grp1"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="elem1" type="grp1"/>
</xsd:schema> 

It is also "entertaining" to see that the following schema actually works with xsd.exe:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:annotation>
        <xsd:documentation xmlns="">Generated from "Set1" under "Release2"</xsd:documentation>
    </xsd:annotation>
    <xsd:element name="elem1">
        <xsd:complexType>
            <xsd:group ref="grp1"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:group name="grp1">
        <xsd:sequence>
            <xsd:element minOccurs="0" name="elem2">
                <xsd:complexType>
                    <xsd:group ref="grp1"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:group>
</xsd:schema>

From an XML instance perspective, all three valid schemas are equivalent.




回答2:


The issue is probably that the tools you are using don't support all possibilities supported by the XML schema spec. Certainly xsd.exe doesn't support everything. The spec is gigantic and it isn't worth providing mappings from everything it supports into a programming language, particularly when some things just don't map very well.

To work around this, you could try to create a set of C# classes that mimic the xml you want to generate and then run xsd.exe on those classes to generate an xsd. There is probably some other XML schema construct that supports what you want.




回答3:


It's a legal scheme. Problem is that xsd is trying to traverse all dependencies. The MS version preprocesses scheme and expands all groups. Because of the cyclic dependency such expansion would be infinite so it quits with an error. With the Mono version there are two probable scenarios:

  1. It tries to traverse dependency tree and ends up in an infinite loop.
  2. It tries to expand all groups and ends up in an infinite loop.

That is just my guess. I never saw actual source codes of Mono xsd.




回答4:


I don't know about groups but XSD.exe supports circular elements:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Class1" nillable="true" type="Class1" />
  <xs:complexType name="Class1">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="child" type="Class1" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>


来源:https://stackoverflow.com/questions/4048945/are-circular-groups-allowed-by-xsd-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!