這改變了一些東西,並且編譯了XML。有兩種方法ComplexType繼承或替代組。複雜類型更適合breif。
![enter image description here](https://i.stack.imgur.com/qqIl4.png)
<?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的:在目錄類型= 「DirectoryType」 屬性。這告訴XML解析器該內容是由DirectoryType定義的。沒有這個文件。這是你的規格說的,但它會非常凌亂。
另一種解決方案是使用替換組,我個人不喜歡替換組,因爲它們在模式中難以閱讀。這種效果與原始解決方案相同。
![enter image description here](https://i.stack.imgur.com/lzb0E.png)
<?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>
爲什麼是目錄中的文件?除了這個想法是你提供你目前的努力,我們的幫助。這不是我們爲15分的重要盼望而爲你做的工作。 –
從文件系統的角度來看,目錄只是另一個文件,其內容是文件名到索引節點的映射,因此它們都具有相似的屬性,如名稱,大小,權限,更改時間等。我在想,在真正的C++/Java/C#代碼中,這樣的概念應該用繼承/遞歸結構輕鬆建模。實際上,我的意圖是在C#/ C++/Java中在XML和這些數據結構之間來回轉換。 – user1943928
地圖不是領土。就物理存儲系統而言,目錄是一個文件,但您沒有指出您的模型包含該功能的理由。如果我爲此選擇了繼承路線(不太可能是誠實的),我會讓它們都繼承自PhysicalFile Class。儘管如此,我更願意在這種情況下使用聚合。 –