1

ファイルとディレクトリに似た構造を記述する XSD スキーマを定義するユース ケースがあります。

  1. File複数の属性を持つ複合型です。
  2. Directoryは であるFileため、 の拡張、Fileつまり から継承されたものである必要がありFileます。
  3. DirectorymoreFilesと subを含むことができるDirectoriesので、再帰的に定義する必要があります。
  4. File要素である可能性があります。
  5. Directoryルート要素になる可能性があります。

誰でもスキーマ ファイルの例を提供できますか?

たとえば、次の C++ コードは正常にコンパイル/実行されます。

    #include <iostream>
    #include <vector>

    using namespace std;

    class FileClass {
    public:
       string name;
       int   type;
       string path;
       long long size;
       virtual int open(){
       };
       virtual int close (){
       };
       virtual ~FileClass(){
       };
    };

    class DirectoryClass: public FileClass {
    public:
       vector<FileClass*> dir_contents;
    };

    int main() {
    cout<<"started"<<endl;
    DirectoryClass root;
    root.open();
    FileClass* newFile = new FileClass();
    root.dir_contents.push_back(newFile);
    DirectoryClass* subDir = new DirectoryClass();
    root.dir_contents.push_back(subDir);
    root.close();
    cout<<"finished"<<endl;
    }

ご覧のとおり、DirectoryClass には FileClass のベクトルが 1 つだけ含まれており、DirectoryClass は FileClass から派生しているため、サブディレクトリを格納できます。

4

2 に答える 2

1

私はトニーに同意します、なぜファイルからディレクトリを派生させるのですか?しかし、それがあなたがモデル化しようとしているものなら...

XSDのグラフィック表現

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition 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:complexType>
    <xs:element name="File"
                type="FileType" />
    <xs:element name="Directory">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="FileType">
                    <xs:sequence>
                        <xs:element ref="File"
                                    minOccurs="0"
                                    maxOccurs="unbounded" />
                        <xs:element ref="Directory"
                                    minOccurs="0"
                                    maxOccurs="unbounded" />
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>
</xs:schema>

次に、C#、C ++、Javaなどから構造化されたXMLデータを読み書きしようとしている場合は、XMLデータバインディングを確認できます。これにより、XSDから強く型付けされたオブジェクトのセットが生成されます。

于 2013-01-03T09:49:26.103 に答える
0

これにより、状況が少し変わり、XML が複雑になります。ComplexType 継承または置換グループの 2 つのアプローチがあります。複雑な型は、簡単に言えばより適切です。

ここに画像の説明を入力

<?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" 属性の使用に注意してください。これにより、コンテンツが DirectoryType によって定義されていることが 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)-->
<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>
于 2013-01-08T14:39:32.820 に答える