这会稍微改变一些东西并使 XML 变得复杂。有 2 种方法 ComplexType 继承或替换组。复杂类型更适合 breif。
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="FileType">
<xs:attribute name="name"
type="xs:string"
use="required" />
<xs:attribute name="creationDate"
type="xs:dateTime"
use="required" />
<xs:attribute name="type"
type="xs:int" />
<xs:attribute name="size"
type="xs:long" />
</xs:complexType>
<xs:complexType name="DirectoryType">
<xs:complexContent>
<xs:extension base="FileType">
<xs:sequence>
<xs:element name="Content"
type="FileType"
minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Directory"
type="DirectoryType" />
</xs:schema>
示例 XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Directory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FoldersCT.xsd"
name="string"
creationDate="1975-12-12T08:25:00.94"
type="393"
size="-5191">
<Content name="FileA1"
creationDate="1993-02-23T21:13:52.11"
type="4931"
size="-1984" />
<Content name="FileA2"
creationDate="1991-12-26T15:57:44.30"
type="-6155"
size="18" />
<Content name="FileA3"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579" />
<Content xsi:type="DirectoryType"
name="DirA4"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579">
<Content name="FileB1"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579" />
<Content name="FileB2"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579" />
</Content>
</Directory>
请注意在目录中使用 xsi:type="DirectoryType" 属性。这告诉 XML Parser 内容是由 DirectoryType 定义的。没有这个它的文件。这就是您的规范所说的,但我会说它非常混乱。
另一种解决方案是使用替换组,我个人不喜欢它,因为它们很难在模式中阅读。这与原始解决方案相同。
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="FileType">
<xs:attribute name="name"
type="xs:string"
use="required" />
<xs:attribute name="creationDate"
type="xs:dateTime"
use="required" />
<xs:attribute name="type"
type="xs:int" />
<xs:attribute name="size"
type="xs:long" />
</xs:complexType>
<xs:element name="File"
type="FileType"
substitutionGroup="File" />
<xs:element name="Directory"
substitutionGroup="File">
<xs:complexType>
<xs:complexContent>
<xs:extension base="FileType">
<xs:sequence>
<xs:element ref="File"
minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
示例 XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Directory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FoldersSG.xsd"
name="string"
creationDate="1992-01-13T17:58:08.38"
type="-2296"
size="-8752">
<File name="string"
creationDate="1982-11-03T17:57:40.96"
type="6813"
size="5278" />
<File name="string"
creationDate="1986-12-08T12:38:03.46"
type="4479"
size="8453" />
<Directory name="string"
creationDate="1992-01-13T17:58:08.38"
type="-2296"
size="8752">
<File name="string"
creationDate="1982-11-03T17:57:40.96"
type="6813"
size="5278" />
<File name="string"
creationDate="1986-12-08T12:38:03.46"
type="4479"
size="8453" />
</Directory>
</Directory>