rspec/rules/S3857/xml/rule.adoc

61 lines
1.9 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Complex types can be restricted by means of an ``++xs:restriction++`` element. But ``++restriction++`` is anti-inheritance: the derived type has less information than the base type. Further, restriction causes the redefinition of the complex types elements and attributes, which leads to fragility in your XML schema; any change to a super type will require corresponding changes in all restricting subtypes, even those in other schemas. For example, if the base type defines a mandatory "color" element of type ``++xs:int++``, the restricting type must also define a "color" element whose type cannot be, say, ``++xs:string++``.
``++xs:extension++``, on the other hand, allows the extending element to inherit the base class' elements and add new ones.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,xml]
2021-04-28 16:49:39 +02:00
----
<xs:complexType name="Fruit>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="color" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="UncoloredFruit">
<xs:complexContent>
<xs:restriction base="Fruit"> <!-- Noncompliant -->
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,xml]
2021-04-28 16:49:39 +02:00
----
<xs:complexType name="FruitBase>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ColoredFruit>
<xs:sequence>
<!-- Bottom-up design, using aggregation -->
<xs:element name="base" type="FruitBase"/>
<xs:element name="color" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]