XML Schema: Can I make some of an attribute's values be required but still allow other values?

Posted by scrotty on Stack Overflow See other posts from Stack Overflow or by scrotty
Published on 2010-05-05T23:00:48Z Indexed on 2010/05/05 23:08 UTC
Read the original article Hit count: 245

Filed under:
|
|
|

(Note: I cannot change structure of the XML I receive, I am only able to change how I validate it.)

Let's say I can get XML like this:

<Address Field="Street" Value="123 Main"/>
<Address Field="StreetPartTwo" Value="Unit B"/>
<Address Field="State" Value="CO"/>
<Address Field="Zip" Value="80020"/>
<Address Field="SomeOtherCrazyValue" Value="Foo"/>

I need to create an XSD schema that validates that "Street", "State" and "Zip" must be present. But I don't care if "StreetPartTwo" or "SomeOTherCrazyValue" is present.

If I knew that only the three I care about could be included, I could do this:

<xs:element name="Address" type="addressType" maxOccurs="unbounded" minOccurs="3"/>

<xs:complexType name="addressType">
  <xs:attribute name="Field" use="required">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="Street"/>
        <xs:enumeration value="State"/>
        <xs:enumeration value="Zip"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
</xs:complexType>

But this won't work with my case because I may also receive those other Address elements (that also have "Field" attributes) that I don't care about.

Any ideas how I can ensure the stuff I care about is present but let the other stuff in too?

TIA! Sean

© Stack Overflow or respective owner

Related posts about xsd

Related posts about xml-schema