简体   繁体   English

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?

This was most horrible question title ever, but I hope somebody has time to answer before it's closed. 这是有史以来最恐怖的问题标题,但我希望有人在关闭之前有时间回答。

EDIT: Changed title. 编辑:更改标题。 EDIT2: There is approx. EDIT2:有大约。 300 component types eg tables. 300种组件类型,例如表格。 And the list changes all the time. 并且列表一直在变化。 If totally dynamic approach is not possible, also string-enumeration could be acceptable because adding new table to this enumerations is easy and should be backward compatible. 如果不可能采用完全动态的方法,那么字符串枚举也是可以接受的,因为向此枚举添加新表很容易,并且应向后兼容。

A customer wants me to change my current schema in a way that xml: 客户希望我以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>

would look like this: 看起来像这样:

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

My schema definition is like: 我的架构定义如下:

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

I'm interested 我很感兴趣

  • if it's possible without defining a complex type for each of the component types? 是否有可能不为每个组件类型定义复杂类型? (I'm not gonna do that, cause it makes no sense.) (我不会这样做,因为这没有任何意义。)

Here you are: 这个给你:

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

Above is a valid schema for the input you've mentioned: 上面是您提到的输入的有效架构:

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

It's not possible to have a static schema that constrains the types of the child elements without constraining their names. 不可能有一个静态模式来约束子元素的类型而不约束它们的名称。 But does the schema have to be immutable? 但是架构必须是不变的吗? You could make Component the head of a substitution group, and you could xs:include a module which defines each of the permitted element names as a member of that substitution group, and you could change that included module whenever the list of names changes; 您可以使Component成为替换组的头,并且可以xs:include一个模块,该模块将每个允许的元素名称定义为该替换组的成员,并且只要名称列表发生更改,就可以更改该包含的模块。 if appropriate you could generate the module from a list held in any convenient form. 如果合适,您可以从以任何方便形式保存的列表中生成模块。

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

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