与<complexType>从一个 XSD 扩展到另一个 XSD 相关的查询



假设我在standard.xsd文件下面有一段代码,

<xs:complexType name="StdBasicTags">
        <xs:sequence>
            <xs:element name="Name" type="xs:string" nillable="true">
                <xs:annotation>
                    <xs:documentation>Name</xs:documentation>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

现在我有了另一个XSD文件,它包含了上面的文件,并向上面的complexType StdBasicTags 添加了一些额外的元素

文件名是fullStandard.xsd,我扩展StdBasicTags的方式如下:

<xs:include schemaLocation="standard.xsd"/>
........
........
    <xs:complexType name="FullStdBasicTags">
            <xs:complexContent>
                <xs:extension base="StdBasicTags">
                    <xs:sequence>
                        <xs:element name="Surname" type="xs:string">
                            <xs:annotation>
                                <xs:documentation>Last name</xs:documentation>
                            </xs:annotation>
                        </xs:element>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

,我有以下问题

  1. 我使用许可的Altova XmlSpy工具为fullStandard.xsd文件生成Sample xml,但我只能看到Name元素,而不能看到预期的Surname元素。有人能说出可能的原因是什么吗?我错过了什么?

  2. 为了扩展StdBasicTags并添加新元素,我不得不给出另一个名称FullStdBasicTags。在这种情况下,完整的最终XML文件的输出是什么?其思想是将NameSurname都放在相同的父complexType StdBasicTags下。

  3. 最后,我期望的体系结构/输出有什么问题吗?

注意**这两个文件都得到了正确的验证。另外,这个想法是在不更改standard.xsd文件的情况下实现我刚才提到的内容。

**更新我很惊讶,直到现在还没有人给我任何建议。与此同时,我尝试了这个,

   <xs:element name="FullNewRoot">
    <xs:complexType>
      <xs:sequence>
          <xs:element ref="rootElementName"/>
      </xs:sequence>
    </xs:complexType>
   </xs:element>

其中rootElementName是standard.xsd的根元素。添加后,我现在可以看到其他元素了。但现在的问题是,整个standard.xsd结构首先出现,然后我在fullStandard.xsd中重新定义的任何东西都会出现,而我只想要所有这些。

请提出一些建议。

请在下面找到问题的答案。

  1. 可能的原因是元素的类型。如果使用StdBasicTags,则类型应为FullStdBasicTag。

  2. 父类型不能引用在子类型中添加的新属性。同样的继承原则也适用于此。

请在下面找到样品。

标准架构.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/StdSchema"
    xmlns:tns="http://www.example.org/StdSchema" elementFormDefault="qualified">
    <complexType name="StdBasicTags">
        <sequence>
            <element name="Name" type="string" nillable="true">
                <annotation>
                    <documentation>Name</documentation>
                </annotation>
            </element>
        </sequence>
    </complexType>
</schema>

完整架构.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/FullSchema"
    xmlns:tns="http://www.example.org/FullSchema" elementFormDefault="qualified"
    xmlns:tst="http://www.example.org/StdSchema">
    <include schemaLocation="StdSchema.xsd" />
    <import schemaLocation="StdSchema.xsd" namespace="http://www.example.org/StdSchema"></import>
    <complexType name="FullStdBasicTags">
        <complexContent>
            <extension base="tst:StdBasicTags">
                <sequence>
                    <element name="Surname" type="string">
                        <annotation>
                            <documentation>Last name</documentation>
                        </annotation>
                    </element>
                </sequence>
            </extension>
        </complexContent>
    </complexType>
    <element name="TestTag" type="tns:FullStdBasicTags"/>
</schema>

示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<tns:TestTag xmlns:tns="http://www.example.org/FullSchema" xmlns:tns1="http://www.example.org/StdSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/FullSchema FullSchema.xsd ">
  <tns1:Name>tns1:Name</tns1:Name>
  <tns:Surname>tns:Surname</tns:Surname>
</tns:TestTag>

最新更新