New to XSD and XML, need to define certain elements as required and the other ones as optional

Hi, I am very new to XML Schema and I have the following question. Lets say I am trying to create a Schema for a 'Person' whose information include (SSN, name, address, phone). SSN and name are required with only 1 occurrence of them, whereas address and phone are optional with 0 or 1 occurrences. Could someone show me a simple example of how this is done.

thanks in advance
dmehranAsked:
Who is Participating?
 
anthonywjones66Commented:
The maxOccurs are there to be explicit but since maxOccurs and miOccurs both default to 1 this can simplyfied to:-

     <xs:element name="Person">
          <xs:complexType>
               <xs:sequence>
                    <xs:element name="SSN" type="xs:string"  />
                    <xs:element name="address" type="xs:string" minOccurs="0" />
                    <xs:element name="phone" type="xs:string" minOccurs="0"  />
               </xs:sequence>
          </xs:complexType>
     </xs:element>

Anthony.
0
 
anthonywjones66Commented:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified"
      xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Person">
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="SSN" type="xs:string" minOccurs="1" maxOccurs="1" />
                        <xs:element name="address" type="xs:string" minOccurs="0" maxOccurs="1" />
                        <xs:element name="phone" type="xs:string" minOccurs="0" maxOccurs="1" />
                  </xs:sequence>
            </xs:complexType>
      </xs:element>
</xs:schema>
0
 
dmehranAuthor Commented:
cool, I figured that the answer must be simple but I was a bit confused since i wasn't sure whether I needed to use attributes in order to define the elements as qualified or optional.

thanka again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.