开发者

Is it possible to remove this redundancy from my XML schema?

开发者 https://www.devze.com 2023-03-25 06:43 出处:网络
I have the following problem: I have two complexTypes, foo and bar with both the member foobar: <xs:complexType name=\"foo\">

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号