Is it possible to remove this redundancy from my XML schema?
I have the following problem:
I have two complexTypes, foo
and bar
with both the member foobar
:
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="foobar" type="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="bar">
<xs:sequence>
<xs:element name="foobar" type="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
To reduce redundancy, I want to declare a foobar as a complexType, which gets referenced in both foo
and bar
, something like this:
<xs:complexType name="foobar">
<x开发者_C百科s:sequence>
<xs:element type="xs:string />
</xs:sequence>
</xs:complexType>
But this doesn't seem to be the right way. Is this actually possible in XML Schema?
You are probably better off using an xs:group
element for that rather than a complex type:
<xs:complexType name="foo">
<xs:group ref="foobarGroup"/>
</xs:complexType>
<xs:complexType name="bar">
<xs:group ref="foobarGroup"/>
</xs:complexType>
<xs:group name='foobarGroup'>
<xs:sequence>
<xs:element name="foobar" type="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:group>
You can't express the sequence directly as part of an xs:complexType
.
I have yet to check the following for correctness, but I believe you can do it like this:
<xs:complexType name="hasFoobar" abstract="true">
<xs:sequence>
<xs:element name="foobar" … />
</xs:sequence>
</xs:complexType>
And then "extend" this abstract type:
<xs:complexType name="foo">
<xs:complexContent>
<xs:extension base="hasFoobar">
…
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="bar">
<xs:complexContent>
<xs:extension base="hasFoobar">
…
</xs:extension>
</xs:complexContent>
</xs:complexType>
精彩评论