Link to home
Start Free TrialLog in
Avatar of AussieSilver
AussieSilver

asked on

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

Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

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

Avatar of AussieSilver
AussieSilver

ASKER

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

Maybe you need to define a type for each?

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

How is it not working?
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
Any suggestions???
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" />
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

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>
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

ASKER CERTIFIED SOLUTION
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial