4

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?

I am aware this could be accomplished in XML Schema 1.1 using <xs:assert/>, but unfortunately I am stuck 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>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Kevin Albrecht
  • 6,974
  • 7
  • 44
  • 56

3 Answers3

0

It seems to me that this is a "has" relationship.

If you have a PhoneNumber element, then it should have a property that is of type PhoneNumberType. Rather than messing around with validating and restrictions, I would suggest that you turn PhoneNumber into a complex element and make PhoneNumberType a required property of it.

womp
  • 115,835
  • 26
  • 236
  • 269
0

May be too late, but you can put them in group like this

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

And make this group required or not

Shikarn-O
  • 3,337
  • 7
  • 26
  • 27