如何使用新元素扩展第二个XSD模式

问题描述

我有两个xsd -s,可用来验证两种类型的xml。一个xsd是从第一个xsd扩展而来的,我需要对此xsd进行验证的xml可以包含元素,来自第一个xsd以及来自此扩展的xsd的属性

这是我第一个xsd的一部分

<xs:complexType name="ModelType">
  <xs:annotation>
    <xs:documentation>
          This is the root model type.
          It is a container for the elements,relationships,diagrams and organizations of the model.
      </xs:documentation>
  </xs:annotation>
  <xs:complexContent>
    <xs:extension base="NamedReferenceableType">
      <xs:sequence>

        <xs:group ref="PropertiesGroup" />

        <xs:element name="Metadata" type="MetadataType" minOccurs="0" maxOccurs="1">
          <xs:annotation>
            <xs:documentation>The "Metadata" element is the optional Meta-data for the model.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="elements" type="ElementsType" minOccurs="0" maxOccurs="1">
          <xs:annotation>
            <xs:documentation>The "elements" element is optional and is a container for all elements.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="relationships" type="RelationshipsType" minOccurs="0" maxOccurs="1">
          <xs:annotation>
            <xs:documentation>The "relationships" element is optional and is a container for all relationships.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="organizations" type="OrganizationsType" minOccurs="0" maxOccurs="unbounded">
          <xs:annotation>
            <xs:documentation>The "organizations" element is optional and is a container for the tree nodes of the different structural organization of model elements and relationships.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="propertyDeFinitions" type="PropertyDeFinitionsType" minOccurs="0" maxOccurs="1" />
        <!--<xs:element name="exporttype" type="xs:string" />-->

      </xs:sequence>

      <xs:attribute name="version" type="xs:string" use="optional">
        <xs:annotation>
          <xs:documentation>Specifies the version of the model.</xs:documentation>
        </xs:annotation>
      </xs:attribute>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

我需要第二个xsd包含一个名为exporttype的元素,该元素应该是我的第一个ModelType的{​​{1}}的子元素。我需要使用第一个xsd进行验证的xml不应包含此xsd元素,但是第二个xml将包含exporttype元素。我需要验证一下。

exporttype

我已经尝试过将类似的内容放入第二个xsd

<?xml version="1.0"?>
<model xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="id-5f48f73ea7067b68f0dbf81f" xmlns="http://www.opengroup.org/xsd/archimate/3.0/">
  <name xml:lang="EN"></name>
  <exporttype>test</exporttype>
  <views>
    <diagrams>
      <view identifier="id-3710ccd2-e603-4002-bea5-2033788047b9">
        <name xml:lang="EN">shapes</name>
        <node xmlns:q1="" xsi:type="q1:Shape" identifier="id-a969ed9d-a0d6-47cb-8d8b-045cf32adb61" x="290" y="30" w="1" h="1" nameinternal="" shapeId="5dad5bcd4527ecc5c8c49256" angle="0" isgroup="False" alignment="" textalign="0" size="72 72">
          <label xml:lang="EN" />
          <style>
            <fillColor r="209" g="210" b="212" />
            <font name="Lato" size="13">
              <color r="0" g="0" b="0" />
            </font>
          </style>
        </node>
        <node xmlns:q2=" xsi:type=" q2:Shape" identifier=" id-5397a6f6-b1bf-428d-af84-8ad69468951d" x=" 10" y=" 32" w=" 1" h=" 1" nameinternal=" 5dad54fd0c0ba639c4a5b50c" angle=" 0" isgroup=" False" alignment=" 0" size=" 72 87,171">
          <label xml:lang=" EN" />
          <style>
            <fillColor r=" 209" g=" 210" b=" 212" />
            <font name=" Lato" size=" 13">
              <color r=" 0" g=" 0" b=" 0" />
            </font>
          </style>
        </node>
      </view>
    </diagrams>
  </views>
</model>

为此,我得到了exportype不是模型类型的子项

我也尝试过这样的事情:

   <xs:element name="exporttype" type="xs:string"/>

感谢您的帮助

解决方法

您必须做出选择:

a)将额外元素添加为原始架构中的可选元素 这将始终允许导出标签,但不是强制性的。

b)创建一个单独的元素声明'ModelTypeExtended',其中包含额外的标记。需要原始内容时,请使用ModelType作为您的根标记。要包含新标记时,请使用ModelTypeExtended。

c)创建一个新的复杂类型'barType',以扩展原始的复杂类型。如果要针对扩展版本进行验证,请使用<Model xsi:type="barType" ...以确保XSD验证程序选择该类型的扩展版本。

d)仔细控制您的应用程序使用哪个XSD(不推荐)

除非选择选项d),否则您不能使用相同的标签名称并具有不同的验证规则,但是几乎没有人这样做。