Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an xsd element mandatory

Tags:

xml

xsd

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>
like image 276
Chandru Nagrani Avatar asked Sep 18 '14 07:09

Chandru Nagrani


People also ask

How do you make an element mandatory in XSD?

The "use" property in the XSD definition is used to specify if the attribute is optional or mandatory. To specify that an attribute must be present, use="required" (Note: use may also be set to "prohibited", but we'll come to that later).

Which attribute of schema is mandatory?

That is: the attribute "option" with some unknown value should be mandatory in any document using the complex type containing this element.

How do I make an XML element optional?

To include this optional field as part of the default XML for a new form, leave the box checked. To exclude it, clear the check box. All of the non-optional elements have a grayed-out check box, meaning you cannot clear the check box.

What does minOccurs mean in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.


1 Answers

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>
like image 128
Xstian Avatar answered Oct 05 '22 10:10

Xstian