简体   繁体   English

XSD:重用complexType的子元素吗?

[英]XSD: reuse sub-elements of complexType?

I have defined the following two types in my XSD-file: 我在XSD文件中定义了以下两种类型:

<complexType name="primitive">
  <attribute name="seq_num" type="int"/>
  <element name="prim_to" type="int"/>
</complexType>


<complexType name="configdata">
  <sequence>
    <element name="enable" type="boolean"/>
    <element name="type" default="int"/>
   </sequence>
</complexType>

Both types are used in a number of definitions, ie I would rather not change these them. 这两种类型都用于许多定义中,即我不希望更改它们。 I would like to define a new element set which extends primitive and contains all sub-elements of configdata . 我想定义一个新的元素set ,该元素set扩展primitive并包含configdata所有子元素。 The XML-file for this element would look like this (please note, that enable and type are at the same level as prim_to ): 该元素的XML文件如下所示(请注意, enabletypeprim_to处于同一级别):

<set seq_num="1234">
  <prim_to>22</prim_to>
  <enable>true<enable>
  <type>42</type>
</set>

I could declare set the following way: 我可以通过以下方式声明set

<element name="set">
  <complexType>
    <complexContent>
      <extension base="primitive">
        <sequence>
          <element name="config" type="configdata"/>
        </sequence>
      </extension>
    </complexContent>
  </complexType>
</element>

In this case the XML-file would look like this: 在这种情况下,XML文件将如下所示:

<set seq_num="1234">
  <prim_to>22</prim_to>
  <config>
    <enable>true<enable>
    <type>42</type>
  </config>
</set>

My challenge is to define set in such a way that it extends primitive and contains all sub-elements of configdata - but does not contain an element of the type configdata . 我的挑战是以这种方式定义set ,使其扩展primitive并包含configdata所有子元素-但不包含configdata类型的configdata Basically for the XML-file above it is a question of not having the two 'config'-tags. 基本上,对于上面的XML文件,这是一个没有两个'config'标签的问题。 Is this possible in XSD? 在XSD中可以吗? I would highly appreciate any hints. 我将不胜感激任何提示。

Thanks in Advance, 提前致谢,
Witek 威泰克

A type can only extend a single other one (in other words, there is no multiple inheritance). 一个类型只能扩展单个其他类型(换句话说,没有多重继承)。 So, your set element can extend configdata (with xsd:extension) and have additional elements, or it can extend another type and you could copy configdata elements to extend it. 因此,您的set元素可以扩展configdata(使用xsd:extension)并具有其他元素,或者它可以扩展另一种类型,您可以复制configdata元素来对其进行扩展。 You can use groups to avoid redundancy. 您可以使用组来避免冗余。

You should define the set element like this: 您应该像这样定义set元素:

<element name="set" type="configdata" />

Using this structure, the set element will contain the two child elements in the configdata complex type. 使用此结构, set元素将包含configdata复杂类型中的两个子元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM