Xsd 如何在XML模式中同时使用递归和继承?

Xsd 如何在XML模式中同时使用递归和继承?,xsd,Xsd,我有一个用例来定义XSD模式,以描述类似于文件和目录的结构 文件是一种具有多个属性的复杂类型 目录是一个文件,因此它应该是文件的扩展名,或者换句话说,它继承自文件 目录可以包含更多的文件和子目录,因此应该递归定义它 文件可以是元素 目录可以是根元素 任何人都可以提供一个示例模式文件吗 例如,下面的C++代码编译/运行良好: #include <iostream> #include <vector> using namespace std;

我有一个用例来定义XSD模式,以描述类似于文件和目录的结构

  • 文件
    是一种具有多个属性的复杂类型
  • 目录
    是一个
    文件
    ,因此它应该是
    文件
    的扩展名,或者换句话说,它继承自
    文件
  • 目录
    可以包含更多的
    文件
    和子
    目录
    ,因此应该递归定义它
  • 文件
    可以是元素
  • 目录
    可以是根元素
  • 任何人都可以提供一个示例模式文件吗

    例如,下面的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;
        }
    
    #包括
    #包括
    使用名称空间std;
    类文件类{
    公众:
    字符串名;
    int型;
    字符串路径;
    长尺寸;
    虚拟int open(){
    };
    虚拟整数关闭(){
    };
    虚拟~FileClass(){
    };
    };
    类目录类:公共文件类{
    公众:
    矢量目录内容;
    };
    int main(){
    
    我同意Tony的观点,为什么要从文件中派生目录? 但如果这就是你想要模仿的

    <?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数据,你可以查看。这会从XSD中生成一组强类型的对象。

    < P>我同意托尼的观点,为什么要从文件中导出目录? 但如果这就是你想要模仿的

    <?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数据,你可以从中看到一组强类型的对象,这是由XSD、

    < P>这一点改变了一些东西,并编译了XML。有2种方法:复杂类型继承或替换组。复杂类型更适合于BRIF。
    <?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解析器内容是由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>
    

    它稍微改变了一些事情并编译了XML。有两种方法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解析器内容是由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>
    
    
    
    为什么目录是一个文件?除了您提供当前的工作之外,我们还提供帮助。我们并不是在徒劳地希望获得15个重复点的情况下为您完成工作。从文件系统的角度来看,目录只是另一个文件,其内容是文件名到索引节点的映射,因此它们都具有类似的属性,例如名称、大小、权限等nge时间等。我认为在真正的C++/Java/C#代码中,这种概念应该很容易用继承/递归结构建模。实际上,我的意图是在XML和C++/C++/Java中的这种数据结构之间来回转换。地图不是领域。你指出,就物理存储系统而言,目录是一个文件但是,您的模型没有理由包含该功能。如果我为此选择了继承路径(不太可能是诚实的)我会让它们都从PhysicalFile类继承。在这种情况下,我更可能使用聚合而不是继承。为什么目录是一个文件?除此之外,我们的想法是你提供你当前的努力,我们会提供帮助。我们并不是在徒劳地希望得到15个重复点的情况下为你做工作。从文件系统的角度来看,目录只是另一个文件的内容是文件名到inode的映射,因此它们都具有类似的属性,如名称、大小、权限、更改时间等。我认为在真正的C++/Java/C#代码中,应该使用inh轻松地对此类概念进行建模