是否可以在 XSD 中定义一个元素,该元素在 XML 中可以具有不同的名称,具体取决于它代表的数据类型?

is it possible in XSD to define a element that can have different names in XML depending on what kind of data it represents?

这是有史以来最可怕的问题标题,但我希望有人有时间在它关闭之前回答。

编辑:更改标题。 EDIT2:大约有。 300 种组件类型,例如table秒。并且列表一直在变化。如果完全动态的方法是不可能的,string-enumeration 也可以接受 table 因为向这个枚举添加新的 table 很容易并且应该向后兼容。

一位客户希望我以 xml 的方式更改我当前的架构:

/*0-n components, the outer xml is of no interest in this context */
<component table=ElectricalComponent>
   <id>3</id>
   <class>class1</class>
</component>
<component table=OtherKindOfComponent>
   <id>4</id>
   <class>class2</class>
</component>

看起来像这样:

<electricalcomponent>
   <id>3</id>
   <class>class1</class>
</electricalcomponent>
<otherkindofcomponent>
   <id>4</id>
   <class>class2</class>
</otherkindofcomponent>

我的架构定义如下:

<xs:complexType name="tComponent">
   <xs:sequence>
      <xs:element name="id" type="xs:int" minOccurs="0"/>
      <xs:element name="class" type="xs:string" minOccurs="0"/>
   </xs:sequence>
   <xs:attribute name="table" type="xs:string"/>
</xs:complexType>

<xs:element name="GetComponentResponse">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="component" type="tComponent" />
     </xs:sequence>
   </xs:complexType>
</xs:element>

我有兴趣

给你:

<xs:element name="GetComponentResponse">
    <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:choice>
                <xs:element name="electricalcomponent" type="tComponent" />
                <xs:element name="otherkindofcomponent" type="tComponent" />
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
</xs:element>

以上是您提到的输入的有效架构:

<electricalcomponent>
   <id>3</id>
   <class>class1</class>
</electricalcomponent>
<otherkindofcomponent>
   <id>4</id>
   <class>class2</class>
</otherkindofcomponent>

不可能有一个静态模式来约束子元素的类型而不约束它们的名称。但是模式必须是不可变的吗?您可以使 Component 成为替换组的头,并且可以 xs:include 一个模块,该模块将每个允许的元素名称定义为该替换组的成员,并且您可以在名称列表更改时更改包含的模块;如果合适,您可以从以任何方便形式保存的列表中生成模块。