xml schema - required fields

Hi,

I have the following element:

how can I make lname and fname to be a required fiels and mname to be an optional field?

Cheers,
<xs:element name="name">
	<xs:complexType>
			<xs:sequence>
			 <xs:element ref="lname" /> 
			 <xs:element ref="mname" /> 
			 <xs:element ref="fname" /> 
            </xs:sequence>
	</xs:complexType>
	 </xs:element>

Open in new window

AussieSilverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobinSoftware EngineerCommented:
How about :

(http://www.w3.org/TR/xmlschema-0/)
<xs:element ref="lname" minOccurs="1" maxOccurs="1" />
<xs:element ref="mname" minOccurs="0" maxOccurs="1" />
<xs:element ref="fname" minOccurs="1" maxOccurs="1" />

Open in new window

0
AussieSilverAuthor Commented:
robinu:

The code you provided is not working I have tried with the following code but with no success....
<xs:element name="name">
	<xs:complexType>
			<xs:sequence>
				<xs:element name="lname" minOccurs="1" maxOccurs="1" />
				<xs:element name="mname" minOccurs="0" maxOccurs="1" />
				<xs:element name="fname" minOccurs="1" maxOccurs="1" /> 
            </xs:sequence>
	</xs:complexType>
	 </xs:element>

Open in new window

0
RobinSoftware EngineerCommented:
Maybe you need to define a type for each?

e.g.: type="xsd:string"

How is it not working?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

AussieSilverAuthor Commented:
I tried it with type="xsd:string"...

I mean If I leave the fname blank in the xml file... the validation works with no errors
0
AussieSilverAuthor Commented:
Any suggestions???
0
RobinSoftware EngineerCommented:
Yes. :)

To check for empty strings add the following type in your xsd:

<xs:simpleType name="non-empty-string">
      <xs:restriction base="xs:string">
            <xs:minLength value="1" />
      </xs:restriction>
</xs:simpleType>

and use this type e.g.:

<xs:element name="lname" minOccurs="1" maxOccurs="1" type="non-empty-string" />
0
AussieSilverAuthor Commented:
The element is for the name element... can you add you code to my code... I tried but i gave me some errors .....
<xs:element name="name">
	
		<xs:complexType>
			<xs:sequence>
				<xs:element name="lname" type="xs:string" minOccurs="1"/>
				<xs:element name="mname" type="xs:string" minOccurs="0"/>
				<xs:element name="fname" type="xs:string" minOccurs="1"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>

Open in new window

0
RobinSoftware EngineerCommented:
In your xsd file add in the beginning:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="medicare" />

      <xs:simpleType name="non-empty-string">
            <xs:restriction base="xs:string">
                  <xs:minLength value="1" />
            </xs:restriction>
      </xs:simpleType>

      <xs:element name="medicare-card">
...

and for the name element:

<xs:element name="name">
   <xs:complexType>
      <xs:sequence>
         <xs:element name="lname" minOccurs="1" maxOccurs="1" type="non-empty-string" />
         <xs:element name="mname" minOccurs="0" maxOccurs="1" />
         <xs:element name="fname" minOccurs="1" maxOccurs="1" type="non-empty-string" />
      </xs:sequence>
   </xs:complexType>
</xs:element>
0
AussieSilverAuthor Commented:
I have different beginning ; any way here is my full code and advise me what i should do :) ... sorry for eing so silly .....
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="medicare"> 
        <xs:complexType> 
            <xs:sequence>



	<xs:element name="medicare-card">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="expiry-date"/>
				<xs:element ref="family"/>
			</xs:sequence>
			<xs:attribute name="type" use="required">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:enumeration value="Reciprocal"/>
						<xs:enumeration value="Interim"/>
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
			<xs:attribute name="id" use="required">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:pattern value="[A-Z][0-9]{8}"></xs:pattern>
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
		</xs:complexType>
		<xs:unique name="unique-id">
			<xs:selector xpath="medicare-card"></xs:selector>
			<xs:field xpath="@id"></xs:field>
		</xs:unique>
	</xs:element>

			</xs:sequence> 
</xs:complexType>
</xs:element>

	<xs:element name="expiry-date" type="xs:date"/>
	<xs:element name="family">
		<xs:complexType>
			<xs:sequence minOccurs="1" maxOccurs="9">
				<xs:element ref="person"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>

	<xs:element name="person">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="name"/>
				<xs:element ref="date-of-birth"/>
				<xs:element ref="contact"/>
				<xs:element ref="medical-info"/>
			</xs:sequence>
			<xs:attribute name="gender" use="required">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:enumeration value="Male"/>
						<xs:enumeration value="Female"/>
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
		</xs:complexType>
	</xs:element>



	<xs:element name="name">
	
		<xs:complexType>
			<xs:sequence>
				<xs:element name="lname" type="xs:string" minOccurs="1"/>
				<xs:element name="mname" type="xs:string" minOccurs="0"/>
				<xs:element name="fname" type="xs:string" minOccurs="1"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>


	<xs:element name="date-of-birth" type="xs:date"/>
	<xs:element name="contact">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="address"/>
				<xs:element name="telephone"/>
				<xs:element name="email"/>
			</xs:sequence>
			<xs:attribute name="preferred">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:enumeration value="address"/>
						<xs:enumeration value="telephone"/>
						<xs:enumeration value="email"/>
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
		</xs:complexType>
	</xs:element>
	<xs:element name="address">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="street"/>
				<xs:element ref="city"/>
				<xs:element ref="state"/>
				<xs:element ref="zipcode"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>

	<xs:element name="street" type="xs:string"/>
	<xs:element name="city" type="xs:string"/>
	<xs:element name="state" type="xs:string"/>
	<xs:element name="zipcode">
		<xs:simpleType>
			<xs:restriction base="xs:integer">
				<xs:pattern value="[0-9]{4}"/>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
	<xs:element name="medical-info">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="allergy"/>
				<xs:element ref="chronic-illness"/>
			</xs:sequence>
			<xs:attribute name="type" use="required">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:enumeration value="Opositive"/>
						<xs:enumeration value="Onegative"/>
						<xs:enumeration value="Apositive"/>
						<xs:enumeration value="Anegative"/>
						<xs:enumeration value="Bpositive"/>
						<xs:enumeration value="Bnegative"/>
						<xs:enumeration value="ABpositive"/>
						<xs:enumeration value="ABnegative"/>
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
		</xs:complexType>
	</xs:element>

	<xs:element name="allergy" type="xs:string"/>
	<xs:element name="chronic-illness" type="xs:string"/>
</xs:schema>

Open in new window

0
RobinSoftware EngineerCommented:
Then try this:
<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<xs:simpleType name="non-empty-string">
	<xs:restriction base="xs:string">
		<xs:minLength value="1" />
	</xs:restriction>
</xs:simpleType>
 
<xs:element name="medicare">  
        <xs:complexType>  
            <xs:sequence> 
 
        <xs:element name="medicare-card"> 
                <xs:complexType> 
                        <xs:sequence> 
                                <xs:element ref="expiry-date"/> 
                                <xs:element ref="family"/> 
                        </xs:sequence> 
                        <xs:attribute name="type" use="required"> 
                                <xs:simpleType> 
                                        <xs:restriction base="xs:string"> 
                                                <xs:enumeration value="Reciprocal"/> 
                                                <xs:enumeration value="Interim"/> 
                                        </xs:restriction> 
                                </xs:simpleType> 
                        </xs:attribute> 
                        <xs:attribute name="id" use="required"> 
                                <xs:simpleType> 
                                        <xs:restriction base="xs:string"> 
                                                <xs:pattern value="[A-Z][0-9]{8}"></xs:pattern> 
                                        </xs:restriction> 
                                </xs:simpleType> 
                        </xs:attribute> 
                </xs:complexType> 
                <xs:unique name="unique-id"> 
                        <xs:selector xpath="medicare-card"></xs:selector> 
                        <xs:field xpath="@id"></xs:field> 
                </xs:unique> 
        </xs:element> 
 
                        </xs:sequence>  
</xs:complexType> 
</xs:element> 
 
        <xs:element name="expiry-date" type="xs:date"/> 
        <xs:element name="family"> 
                <xs:complexType> 
                        <xs:sequence minOccurs="1" maxOccurs="9"> 
                                <xs:element ref="person"/> 
                        </xs:sequence> 
                </xs:complexType> 
        </xs:element> 
 
        <xs:element name="person"> 
                <xs:complexType> 
                        <xs:sequence> 
                                <xs:element ref="name"/> 
                                <xs:element ref="date-of-birth"/> 
                                <xs:element ref="contact"/> 
                                <xs:element ref="medical-info"/> 
                        </xs:sequence> 
                        <xs:attribute name="gender" use="required"> 
                                <xs:simpleType> 
                                        <xs:restriction base="xs:string"> 
                                                <xs:enumeration value="Male"/> 
                                                <xs:enumeration value="Female"/> 
                                        </xs:restriction> 
                                </xs:simpleType> 
                        </xs:attribute> 
                </xs:complexType> 
        </xs:element> 
 
        <xs:element name="name"> 
                <xs:complexType> 
                        <xs:sequence> 
                                <xs:element name="lname" type="non-empty-string" minOccurs="1"/> 
                                <xs:element name="mname" type="xs:string" minOccurs="0"/> 
                                <xs:element name="fname" type="non-empty-string" minOccurs="1"/> 
                        </xs:sequence> 
                </xs:complexType> 
        </xs:element> 
  
        <xs:element name="date-of-birth" type="xs:date"/> 
        <xs:element name="contact"> 
                <xs:complexType> 
                        <xs:sequence> 
                                <xs:element ref="address"/> 
                                <xs:element name="telephone"/> 
                                <xs:element name="email"/> 
                        </xs:sequence> 
                        <xs:attribute name="preferred"> 
                                <xs:simpleType> 
                                        <xs:restriction base="xs:string"> 
                                                <xs:enumeration value="address"/> 
                                                <xs:enumeration value="telephone"/> 
                                                <xs:enumeration value="email"/> 
                                        </xs:restriction> 
                                </xs:simpleType> 
                        </xs:attribute> 
                </xs:complexType> 
        </xs:element> 
        <xs:element name="address"> 
                <xs:complexType> 
                        <xs:sequence> 
                                <xs:element ref="street"/> 
                                <xs:element ref="city"/> 
                                <xs:element ref="state"/> 
                                <xs:element ref="zipcode"/> 
                        </xs:sequence> 
                </xs:complexType> 
        </xs:element> 
 
        <xs:element name="street" type="xs:string"/> 
        <xs:element name="city" type="xs:string"/> 
        <xs:element name="state" type="xs:string"/> 
        <xs:element name="zipcode"> 
                <xs:simpleType> 
                        <xs:restriction base="xs:integer"> 
                                <xs:pattern value="[0-9]{4}"/> 
                        </xs:restriction> 
                </xs:simpleType> 
        </xs:element> 
        <xs:element name="medical-info"> 
                <xs:complexType> 
                        <xs:sequence> 
                                <xs:element ref="allergy"/> 
                                <xs:element ref="chronic-illness"/> 
                        </xs:sequence> 
                        <xs:attribute name="type" use="required"> 
                                <xs:simpleType> 
                                        <xs:restriction base="xs:string"> 
                                                <xs:enumeration value="Opositive"/> 
                                                <xs:enumeration value="Onegative"/> 
                                                <xs:enumeration value="Apositive"/> 
                                                <xs:enumeration value="Anegative"/> 
                                                <xs:enumeration value="Bpositive"/> 
                                                <xs:enumeration value="Bnegative"/> 
                                                <xs:enumeration value="ABpositive"/> 
                                                <xs:enumeration value="ABnegative"/> 
                                        </xs:restriction> 
                                </xs:simpleType> 
                        </xs:attribute> 
                </xs:complexType> 
        </xs:element> 
 
        <xs:element name="allergy" type="xs:string"/> 
        <xs:element name="chronic-illness" type="xs:string"/> 
</xs:schema>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.