Limit a type element in a different namespace

I think I need to do this with XSD 1.0, but anyway I will ask ... I have complexType in the file, say a.xsd . Basically, I can not touch this file. In particular, I cannot change its targetNamespace . An example is:

 <xs:schema targetNamespace="http://myns.original" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:orig="http://myns.original"> <xs:element name="config" type="orig:ConfigType"/> <xs:complexType name="ConfigType"> <xs:sequence> <xs:element name="fieldA" type="xs:integer" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema> 

I have a second b.xsd file where I extend the type defined in a.xsd and redefine the element previously defined in a.xsd using substitutionGroup . For now, everything is fine, and the following example looks fine:

 <xs:schema targetNamespace="http://myns.myns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myns="http://myns.myns" xmlns:orig="http://myns.original"> <xs:import namespace="http://myns.original" schemaLocation="a.xsd"/> <xs:complexType name="ConfigType"> <xs:complexContent> <xs:extension base="orig:ConfigType"> <xs:sequence> <xs:element name="fieldB" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="config" type="myns:ConfigType" substitutionGroup="orig:config"/> </xs:schema> 

Problems: One field in the original complexType is optional ( minOccurs=0 ). Now I need to override this type so that this field is required ( minOccurs=1 ). I suggested that this could be achieved with xsd:redefine , so I tried the following:

 <xs:schema targetNamespace="http://myns.myns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myns="http://myns.myns"> <xs:redefine schemaLocation="b.xsd"> <xs:complexType name="ConfigType"> <xs:complexContent> <xs:restriction base="myns:ConfigType"> <xs:sequence> <xs:element name="fieldA" minOccurs="1"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:redefine> </xs:schema> 

But I get the following messages:

  There is not a complete functional mapping between the particles. Error for type 'ConfigType'. The particle of the type is not a valid restriction of the particle of the base. 

Honestly, I do not quite understand these messages, but after some investigation it seems that the actual problem is that the redefined fields should belong to the same namespace as the redefinition. In my case, I am trying to limit the orig:fieldA in the http://myns.original in a file with targetNamespace = "http: //myns.myns". Of course, if the extension continued the type in c.xsd , as I did in b.xsd , there is no problem since I am not trying to change anything from another namespace.

Does anyone know if this can be achieved? One solution is to replicate the definitions, which will be changed in another a_2.xsd file, with the right targetNamespace . But this is a very undesirable and unattainable solution for a complex system.

+4
source share
1 answer

The only problem I see so far is that in the a.xsd you defined:

 <xs:element name="fieldA" type="xs:integer" minOccurs="0"/> 

whereas in the last scheme (redefinition) you have:

 <xs:element name="fieldA" minOccurs="1"/> 

Actually the last declaration means that you implicitly specify xs:anyType for fieldA :

 <xs:element name="fieldA" type="xs:anyType" minOccurs="1"/> 

Remember that when you get a new type by constraint (which is actually what you are overriding), you must fully define the model of the content of the element. But this new content model should be fully consistent with the old.

This is not the case in your last scheme, because earlier according to a.xsd , the fieldA element is fieldA allowed to have an integer value. But now you say you can accept anything. This will undoubtedly cause an error, and the message you received (albeit quite gibberish):

A particle of this type is not an acceptable limitation of a base particle.

seems to be talking about this.

+2
source

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


All Articles