XSD: multiple types with the same element name

I am using xsd to check xml. I need to describe one element with two types.

<xsd:choice> <xsd:element name="num" minOccurs="1" type="xsd:integer" fixed="0"/> <xsd:element name="num" minOccurs="1" type="xsd:positiveInteger"/> </xsd:choice> 

When I check xml with num = 0, the check succeeds, but when I check xml with number num = 1 or higher, the check fails. How to describe this case?

+6
source share
2 answers

I would use xs: nonNegativeInteger for this use case:

 <xs:element name="num" type="xs:nonNegativeInteger"> 

If you want an element to support several types, you can use union:

 <xs:element name="num" default="0"> <xs:simpleType> <xs:union memberTypes="xs:integer xs:positiveInteger" /> </xs:simpleType> </xs:element> 
+5
source

You cannot have two particles of an element in the same complex type with the same name and different types (this rule is called "Approval of elements is consistent" if you want to find it). One of the reasons is that XSD is used not only for verification, but also for data input, for example. in Java data binding.

But I think you are looking for a type of association here.

+3
source

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


All Articles