tags:

views:

77

answers:

1

How can I require the "Address" element?

If I perform an XSD validation on a document with no "Address" it passes, but I would like it to fail.

Is this possible?

http://en.wikipedia.org/wiki/XSD see the example xsd. it hiding my xsd sample

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Recipient" type="xs:string" />
        <xs:element name="House" type="xs:string" />
        <xs:element name="Street" type="xs:string" />
        <xs:element name="Town" type="xs:string" />
        <xs:element name="County" type="xs:string" minOccurs="0" />
        <xs:element name="PostCode" type="xs:string" />
        <xs:element name="Country">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="FR" />
              <xs:enumeration value="DE" />
              <xs:enumeration value="ES" />
              <xs:enumeration value="UK" />
              <xs:enumeration value="US" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
+5  A: 

You want to set minOccurs="1" in its definition.

Example taken from W3C Schools:

<xs:element name="item" maxOccurs="unbounded">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="title" type="xs:string" minOccurs="0"/>
      <xs:element name="note" type="xs:string" minOccurs="1"/>
      <xs:element name="quantity" type="xs:positiveInteger" minOccurs="0"/>
      <xs:element name="price" type="xs:decimal" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The note element must appear at least once under item, but the others don't have to appear.

Welbog
It tried that, gives me an error.
eschneider
Address is your root element. It's always required otherwise there is no XML document. I don't understand your question.
Welbog
The XSD does not require it, if for some reason something else is there it still validates.
eschneider
example please? I don't get what you're saying. The answer to the original question is "minOccurs='1'". Now you have another question but it's not clear what the question is.
Cheeso