0

So, I have a .xsd file that has a lot of repetitions in it.

<xs:complexType name="Square">
  <xs:sequence>
    <xs:element name="Side1" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side2" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side3" type="xs:integer" minOccurs="1"/>
  <xs:element name="Side4" type="xs:integer" minOccurs="1"/>
  </xs:sequence>
  <xs:attribute name="squarename" type="xs:string" use="optional" />
  <xs:attribute name="squarenumber" type="xs:integer" use="optional" />
</xs:complexType>

<xs:complexType name="Rectangle">
  <xs:sequence>
    <xs:element name="Side1" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side2" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side3" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side4" type="xs:integer" minOccurs="1"/>
  </xs:sequence>
  <xs:attribute name="rectanglecolor" type="xs:string" use="optional" />
  <xs:attribute name="rectanglelabel" type="xs:string" use="optional" />
</xs:complexType>

<xs:complexType name="Rhombus">
  <xs:sequence>
    <xs:element name="Side1" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side2" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side3" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side4" type="xs:integer" minOccurs="1"/>
  </xs:sequence>
  <xs:attribute name="rhombosdance" type="xs:string" use="optional" />
  <xs:attribute name="rhombusname" type="xs:string" use="optional" />
</xs:complexType>

Note that

  <xs:sequence>
    <xs:element name="Side1" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side2" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side3" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side4" type="xs:integer" minOccurs="1"/>
  </xs:sequence>

appears in many places.

This seems kinda bad. Is there any way of having something that is a placeholder or something for that sequence. I would make a new type that has all those, but I need to maintain backwards compatibility, so I need the xsd to take the same xml it was able to, so I can't for example turn

<Square>
  <Side1>1</Side1>
  <Side2>1</Side2>
  <Side3>1</Side3>
  <Side3>1</Side3>
</Square>

into

<Square>
  <QuadSides>
    <Side1>1</Side1>
    <Side2>1</Side2>
    <Side3>1</Side3>
    <Side4>1</Side4>
  <QuadSides>
</Square>

I need to still accept the first xml. Is there any way to do this?

4

2 回答 2

1

另一种方法是使用元素组 (使用元素声明的 XSD 组件<xs:group>)。

下面是如何重写您的 XML 模式:

<xs:group name="Sides">
  <xs:sequence>
    <xs:element name="Side1" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side2" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side3" type="xs:integer" minOccurs="1"/>
    <xs:element name="Side4" type="xs:integer" minOccurs="1"/>
  </xs:sequence>
</xs:group>

<xs:complexType name="Square">
  <xs:group ref="Sides"/>
  <xs:attribute name="squarename" type="xs:string" use="optional" />
  <xs:attribute name="squarenumber" type="xs:integer" use="optional" />
</xs:complexType>

<xs:complexType name="Rectangle">
  <xs:group ref="Sides"/>
  <xs:attribute name="rectanglecolor" type="xs:string" use="optional" />
  <xs:attribute name="rectanglelabel" type="xs:string" use="optional" />
</xs:complexType>

<xs:complexType name="Rhombus">
  <xs:group ref="Sides"/>
  <xs:attribute name="rhombosdance" type="xs:string" use="optional" />
  <xs:attribute name="rhombusname" type="xs:string" use="optional" />
</xs:complexType>
于 2013-09-19T00:59:10.790 回答
0

弄清楚了。我可以使用扩展

<xs:complexType name="QuadSides" abstract="true">
  <xs:element name="Side1" type="xs:integer" minOccurs="1"/>
  <xs:element name="Side2" type="xs:integer" minOccurs="1"/>
  <xs:element name="Side3" type="xs:integer" minOccurs="1"/>
  <xs:element name="Side4" type="xs:integer" minOccurs="1"/>
</xs:complexType>

<xs:complexType name="Square">
  <xs:complexContent>
    <xs:extension base="QuadSides">
      <xs:attribute name="squarename" type="xs:string" use="optional" />
      <xs:attribute name="squarenumber" type="xs:integer" use="optional" />
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

等等

于 2013-09-19T00:05:50.253 回答