Xsd recursion of complexTypes

Posted by Hatch on Stack Overflow See other posts from Stack Overflow or by Hatch
Published on 2010-03-23T14:52:02Z Indexed on 2010/03/23 14:53 UTC
Read the original article Hit count: 419

Filed under:
|
|

I am just learning XML/XSD and am struggling with the implementation of an XML-schema which models a folder structure. What I had in mind was defining a complexType for the folder which can have additional folder instances that represent subfolders. Using the xsd schema validator here always returns that the schema is invalid.

I tried defining the complexType up front and then using the ref keyword for subfolders:

<xs:complexType name="tFolder">
<xs:sequence>
  <xs:element name="Path" type="tFolderType" msdata:Ordinal="0" />                    
  <xs:element ref="Folder" minOccurs="0" maxOccurs="unbounded" />
  <xs:element name="File" nillable="true" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
      <xs:simpleContent msdata:ColumnName="File_Text" msdata:Ordinal="0">
        <xs:extension base="xs:string">
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>                              
</xs:sequence>
<xs:attribute name="Type" type="tFolderType" />

As for the element itself:

<xs:element name="Folder" type="tFolder" />

The error returned by the validator is:

"Cannot resolve the name 'Folder' to a(n) 'element declaration' component."

and the error occurs at the line

<xs:element ref="Folder" minOccurs="0" maxOccurs="unbounded" /\>

Defining the complexType within the element itself yields the exact same error:

<xs:element name="Folder">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="Path" type="tFolderType" msdata:Ordinal="0" />
          <xs:element ref="Folder" minOccurs="0" maxOccurs="unbounded" />                            
          <xs:element name="File" nillable="true" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
              <xs:simpleContent msdata:ColumnName="File_Text" msdata:Ordinal="0">
                <xs:extension base="xs:string">
                </xs:extension>
              </xs:simpleContent>
            </xs:complexType>
          </xs:element>                              
        </xs:sequence>
        <xs:attribute name="Type" type="tFolderType" />
      </xs:complexType>
    </xs:element>

What I've read, this kind of recursion should work using ref.

Can anyone tell me what I've done wrong? Maybe the xsd validator is just faulty? If so, does anyone know a better alternative? I've tried using the one from w3.org as well, but it seems to be taken offline...

© Stack Overflow or respective owner

Related posts about xsd

Related posts about xml-schema