XML schema to restrict one field based on another

I have the following scheme that I use to ensure that the person PhoneNumber and PhoneNumberType (Home, Job, etc.) are no longer than 10 characters. However, I want to improve this scheme so that PhoneNumberType not required if PhoneNumber not provided, but is required if PhoneNumber provided. Is there a way to do this in XML Schema 1.0?

I know that this can be done in XML Schema 1.1 using <xs:assert/> , but unfortunately I stick with XML Schema 1.0.

 <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xs:element name="PhoneNumber"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="0"/> <xs:maxLength value="10"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="PhoneNumberType"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="0"/> <xs:maxLength value="10"/> </xs:restriction> </xs:simpleType> </xs:element> </xsd:schema> 
+4
source share
3 answers

It seems to me that this relationship is "there."

If you have a PhoneNumber element, it must have a property of type PhoneNumberType. Instead of tinkering with validation and restrictions, I would suggest that you turn PhoneNumber into a complex element and make PhoneNumberType the required property.

0
source

It may be too late, but you can put them in a group as follows

 <xs:group name="group"> <xs:sequence> <xs:element ref="PhoneNumber"/> <xs:element ref="PhoneNymberType" /> </xs:sequence> 

And make this group mandatory or not

0
source

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


All Articles