简体   繁体   中英

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. 300 component types eg tables. 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:

/*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; if appropriate you could generate the module from a list held in any convenient form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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