-1

I am having an xml request for which I am trying to make tags mandatory. but positioning the tags at respective position confuses me as I am new to xml. I am using in java with SAX parsing validation.

The xml is

<empRequest><id>5</id><fname>Samp</fname></empRequest>

If I need to check 'id' as mandatory

The following is my xsd file. I feel the proper re-positioning is need for validation.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:element name="empRequest">
<xs:complexType>
        <xs:element name="id" type="xs:int">
            <xs:complexType>
                <xs:simpleContent>
                    <xs:extension base="xs:int">
                        <xs:attribute name="id" use="required"> 
                        </xs:attribute>  
                    </xs:extension>
                </xs:simpleContent>
                </xs:complexType>
         </xs:element>
    <xs:element name="fname" type="xs:string"></xs:element>
    </xs:complexType>
</xs:element>

Kindly help me with exact xsd for the request.

kjhughes
  • 106,133
  • 27
  • 181
  • 240

2 Answers2

1

You are missing the xs:sequence tag. Additionally you are trying to allow an attribute id inside the element id. This seems not to be what you want. So assuming that the element id is mandatory and fname is not, this would be the XML schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="empRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:int"></xs:element>
        <xs:element name="fname" type="xs:string" minOccurs="0"></xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
kenda
  • 488
  • 6
  • 16
  • the attribute tag with 'use="required"' is not needed for the 'id' to be mandatory? – Vidyasagar S T G M Sep 22 '16 at 09:42
  • It is not needed for the element `id`. You use the `minOccurs` attribute to define that the element is mandatory. Because the default value for `minOccurs` is 1 you do not have to provide it in the schema. The attribute `use` is for attribute definitions. – kenda Sep 22 '16 at 09:46
  • The processing instruction target matching "[xX][mM][lL]" is not allowed.-------Getting this exception with same xml and xsd which you shared – Vidyasagar S T G M Sep 22 '16 at 10:01
  • 1
    Seems like a different problem: Take a look at this: http://stackoverflow.com/questions/19889132/error-the-processing-instruction-target-matching-xxmmll-is-not-allowed – kenda Sep 22 '16 at 10:35
  • @VidyasagarSTGM: kenda has answered your original question correctly and more. Please [**accept**](http://meta.stackoverflow.com/q/5234/234215) his answer here and post new questions as needed rather than string matters along in the comments. Thanks. – kjhughes Sep 22 '16 at 12:22
0

Use minOccurs="1" inside the element tag:

<xsd:element name="id" type="xs:int" minOccurs="1" maxOccurs="unbounded">