XML Schema to restrict one field based on another

Posted by Kevin Albrecht on Stack Overflow See other posts from Stack Overflow or by Kevin Albrecht
Published on 2010-03-25T18:39:11Z Indexed on 2010/03/25 18:43 UTC
Read the original article Hit count: 540

Filed under:
|
|

I have the following schema, which I use to ensure that a person's PhoneNumber and PhoneNumberType (Home, Work, etc.) is not longer than 10 characters. However, I want to improve this schema so that PhoneNumberType is not required if a PhoneNumber is not provided, but is required if the PhoneNumber is provided. Is there a way to do this in 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>

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xml-schema