...

Unique constraint on complexType instead of element

I have the following XSD structure:

<xs:schema xmlns:ns="http://abc/">
  ...
  <xs:element name="abc">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="map"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  ...
  <xs:element name="map">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="entry" type="ns:MapEntryType" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="entry">
      <xs:selector xpath="entry"/>
      <xs:field xpath="key"/>
    </xs:unique>
  </xs:element>
  <xs:complexType name="MapEntryType">
    <xs:sequence>
      <xs:element name="key" type="xs:string"/>
      <xs:element name="value" type="xs:anyType"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

It does its job.

The element mapshould now be called something else based on what the wrapper is, so sometimes it's a name map, sometimes properties, sometimes options, etc.

Therefore, I want to generalize the element map.

I tried to do the following:

  • Create mapa xs:complexTypeand change refto type.
    • As a result, xs:uniqueit is not accepted or executed
  • Create mapa xs:complexType, change refto, typeand transfer the constraint xs:uniqueon element definitions.
    • This worked, but led to the XSD having a lot xs:uniquepresent in the document.

, , ?

+4
2

, . XSD 1.0, 1.1 ; , " " , , . ( ), .

+2

XSD 1.0 1.1

, xs: , , XSD 1.1, , xs: unique, xs: unique ref = "" . , XSD 1.0, XSD 1.1, .

( ):

<xs:element name="map">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="entry" type="MapEntryType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <!-- Only completely defined once -->
    <xs:unique name="uniqueEntry">
        <xs:selector xpath="entry"/>
        <xs:field xpath="key"/>
    </xs:unique>
</xs:element>

<xs:element name="hashMap">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="entry" type="MapEntryType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <!-- Referenced here and every other time -->
    <xs:unique ref="uniqueEntry"/>
</xs:element>
+2

Source: https://habr.com/ru/post/1609339/


All Articles