22

I have an xml file that I am trying to validate against an xsd file. Works fine. I want to modify the xsd to make an element value mandatory. How do I do this so when I validate the xml file I want it to fail if the element FirstName is blank (<FirstName></FirstName>)

xml File

<?xml version="1.0" encoding="utf-8" ?>
<Patient>
   <FirstName>Patient First</FirstName>
   <LastName>Patient Last</LastName>
</Patient>

xsd File

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Patient">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="FirstName" type="xs:string" />
        <xs:element name="LastName" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Chandru Nagrani
  • 321
  • 1
  • 2
  • 4
  • 1
    Maybe this [question](http://stackoverflow.com/questions/6014507/xml-schema-restriction-pattern-for-not-allowing-empty-strings) discussing how a schema restriction that disallows empty strings will help. – Damien_The_Unbeliever Sep 18 '14 at 07:49
  • @Chandru Nagrani Did you solve this issue? if yes, how did you do it? – Xstian Nov 27 '15 at 15:25

1 Answers1

34

Mandatory

Attribute minOccurs Optional. Specifies the minimum number of times the any element can occur in the parent element. The value can be any number >= 0. Default value is 1. (By default is mandatory)

<!-- mandatory true-->
<xs:element name="lastName" type="xs:string" />
<!-- mandatory false-->
<xs:element name="lastName" type="xs:string" minOccurs="0" />

Not Empty

<xs:element name="lastName" type="xs:string" >
  <xs:simpleType>
     <xs:restriction base="xs:string">
       <xs:minLength value="1"/>
     </xs:restriction>
  </xs:simpleType>
</xs:element>
Xstian
  • 8,184
  • 10
  • 42
  • 72