首页 > 解决方案 > 如何使用新元素扩展第二个 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我的第一个的子元素xsd。我需要首先验证的 xmlxsd不应包含此exporttype元素,但第二个 xml 将包含该exporttype元素。我需要验证这一点。

<?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>

我试过这样的东西来放入第二个xsd

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

为此,我知道 exporttype 不是模型类型的孩子

我也尝试过这样的事情:

 <xs:complexType name="barType">
    <xs:complexContent mixed="false">
      <xs:extension base="archimate:ModelType">
        <xs:sequence>
          <xs:element name="exporttype" type="xs:string" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

任何帮助表示赞赏

标签: xmlxsd

解决方案


你必须做出选择:

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

b) 创建一个包含额外标签的单独元素声明“ModelTypeExtended”。当您想要原始内容时,使用 ModelType 作为您的根标记。如果要包含新标签,请使用 ModelTypeExtended。

c) 创建一个新的复杂类型“barType”,它扩展了原始复杂类型。当您想要针对扩展版本进行验证时,请使用<Model xsi:type="barType" ... 以确保 XSD 验证器选择类型的扩展版本。

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

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


推荐阅读