开发者

Xsd recursion of complexTypes

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="tFol开发者_运维问答derType" 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...


I think this is the way to do it (don't use ref):

<element name="test" type="tns:TestType"></element>

<complexType name="TestType">
    <sequence>
        <element name="test" type="tns:TestType"></element>
    </sequence>
</complexType>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜