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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RobinConnect With a Mentor Software 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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

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

How is it not working?
0
 
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
All Courses

From novice to tech pro — start learning today.