Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are circular groups allowed by XSD schema?

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>
like image 409
Yuri Astrakhan Avatar asked Feb 11 '26 20:02

Yuri Astrakhan


2 Answers

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.

like image 64
CodeRipper Avatar answered Feb 17 '26 11:02

CodeRipper


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.

like image 30
Petru Gardea Avatar answered Feb 17 '26 10:02

Petru Gardea