unable to supress attribute from parent in the child element (xsd restriction)
I would like to supress subjectIdentifier attribute (which is declared in the parent element ConsumerSubjectResponseType) in the child element CombinedConsumerSubjectResponseType. I thought that not repeating the attribute declaration in the child element would be sufficient enough but apparently it is not. Is this possible to do in xsd?
<xs:complexType name="ConsumerSubjectResponseType">
<xs:sequence>
<xs:element name="CustomerReferenceNumber" type="xs:string" minOccurs="0"/>
<xs:element name="Sources" type="xs:string" minOccurs="0"/>
<xs:element name="AdditionalOutputData" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:att开发者_运维技巧ribute name="subjectIdentifier" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="CombinedConsumerSubjectResponseType">
<xs:complexContent>
<xs:restriction base="ConsumerSubjectResponseType">
<xs:sequence>
<xs:element name="Sources" type="xs:string" minOccurs="0"/>
<xs:element name="AdditionalOutputData" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name="Test">
<xs:complexType><xs:sequence>
<xs:element name="A" type="ConsumerSubjectResponseType"></xs:element>
<xs:element name="B" type="CombinedConsumerSubjectResponseType"></xs:element>
</xs:sequence></xs:complexType>
</xs:element>
Is this possible to do in xsd?
In your example case: no, or not in that way.
Types derived by restriction must repeat all the particle components, but attributes don't need to be repeated in the derived type definition, they are inherited from the base type. Removing a required attribute from a restricted type is not possible. You could specify a certain fixed (possibly empty) value for required attributes and optional attributes you could remove by using use="prohibited"
.
Generally what you are trying to do is in a way backwards because "Members of a type, A, whose definition is a restriction of the definition of another type, B, are always members of type B as well" and "A complex type definition which allows element or attribute content in addition to that allowed by another specified type definition is said to be an extension" (quotes from W3C schema definition 1, 2).
Solution 1, use xs:extension
instead
Create ConsumerSubjectResponseType
as a base type and derive CombinedConsumerSubjectResponseType
from it by extension.
<xs:complexType name="CombinedConsumerSubjectResponseType-base">
<xs:sequence>
<xs:element minOccurs="0" name="Sources" type="xs:string" />
<xs:element minOccurs="0" name="AdditionalOutputData" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ConsumerSubjectResponseType-extension">
<xs:complexContent>
<xs:extension base="CombinedConsumerSubjectResponseType-base">
<xs:sequence>
<xs:element minOccurs="0" name="CustomerReferenceNumber" type="xs:string" />
</xs:sequence>
<xs:attribute name="subjectIdentifier" type="xs:string" use="required" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
The problem with this solution is that the order of the elements in the extended type is changed, compared to your original type definition. Elements <Sources>
and <AdditionalOutputData>
appear before the element <CustomerReferenceNumber>
. Reason for this is that the base type and additional definitions are treated as two children of a sequential group and the base type is added first.
Solution 2, use a xs:group
containing common elements
Define the sequence in CombinedConsumerSubjectResponseType
as a group. Then you can create the type CombinedConsumerSubjectResponseType
simply by referring to that group. In ConsumerSubjectResponseType
you add the required new elements and attributes in correct places and also refer to this group.
<xs:group name="CombinedConsumerSubjectResponse-group">
<xs:sequence>
<xs:element minOccurs="0" name="Sources" type="xs:string" />
<xs:element minOccurs="0" name="AdditionalOutputData" type="xs:string" />
</xs:sequence>
</xs:group>
<xs:complexType name="ConsumerSubjectResponseType-group">
<xs:sequence>
<xs:element minOccurs="0" name="CustomerReferenceNumber" type="xs:string" />
<xs:group ref="CombinedConsumerSubjectResponse-group" />
</xs:sequence>
<xs:attribute name="subjectIdentifier" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="CombinedConsumerSubjectResponseType-group">
<xs:group ref="CombinedConsumerSubjectResponse-group" />
</xs:complexType>
In this case, the changes that affect both of the types, should be done on the group, not on the types.
精彩评论