How to make an XSD element necessary or not required depending on the context?

We have a definition for the Person element in which we want to depending on what they do. For example, if they add Person, then different elements are required for sending compared to updating the Person. In the example below, the type Person is currently duplicated, which of course is incorrect. Is there a good way to represent this in xsd so we can reuse the Person type.

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="Person"> <xs:annotation> <xs:documentation>This is the definition when changing a person</xs:documentation> </xs:annotation> <xs:sequence> <xs:element name="PartyName" type="xs:string" minOccurs="0" maxOccurs="1"/> <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="0" maxOccurs="1"/> <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/> </xs:sequence> </xs:complexType> <xs:complexType name="Person"> <xs:annotation> <xs:documentation>This is the definition when adding a person</xs:documentation> </xs:annotation> <xs:sequence> <xs:element name="PartyName" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="1" maxOccurs="1"/> <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/> </xs:sequence> </xs:complexType> </xs:schema> 
+4
source share
1 answer

The easiest way to have two different types for Person elements is to use local Person declarations in two different contexts that you mean. For example, you can say:

 <xs:element name="Add"> <xs:complexType> <xs:sequence> <xs:element name="Person" type="AddPerson"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Update"> <xs:complexType> <xs:sequence> <xs:element name="Person" type="ChangePerson"/> </xs:sequence> </xs:complexType> </xs:element> 

This example assumes that you have redefined two of your complex types: AddPerson and ChangePerson.

If you also want the two complex types to be explicitly connected, you can get them as a restriction on the generic Person type.

 <xs:complexType name="Person"> <xs:annotation> <xs:documentation>This is the generic definition for persons</xs:documentation> </xs:annotation> <xs:sequence> <xs:element name="PartyName" type="xs:string" minOccurs="0" maxOccurs="1"/> <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="0" maxOccurs="1"/> <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/> </xs:sequence> </xs:complexType> <xs:complexType name="ChangePerson"> <xs:annotation> <xs:documentation>This is the definition when changing a person</xs:documentation> </xs:annotation> <xs:complexContent> <xs:restriction base="Person"> <xs:sequence> <xs:element name="PartyName" type="xs:string" minOccurs="0" maxOccurs="1"/> <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="0" maxOccurs="1"/> <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> <xs:complexType name="AddPerson"> <xs:annotation> <xs:documentation>This is the definition when adding a person</xs:documentation> </xs:annotation> <xs:complexContent> <xs:restriction base="Person"> <xs:sequence> <xs:element name="PartyName" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="1" maxOccurs="1"/> <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> 

Here, the generic Person type is identical to AddPerson; I defined AddPerson using an empty constraint only to symmetry the inference of both types related to the operation from the generic type.

Whether such an explicit relationship between your types is really useful for achieving your goals will depend, of course, in part on what your system uses to determine the types of circuitry.

+4
source

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


All Articles