Click here to Skip to main content
15,885,981 members
Please Sign up or sign in to vote.
3.20/5 (2 votes)
See more:
<?xml version="1.0" encoding="UTF-8"?>
 <Metadata>
  <Field Name="Claim Number">100</Field>
  <Field Name="Audit Year">2010</Field>
  <Field Name="Auditor Name">ABC PQR</Field>
</Metadata>


in the above xml if I delete <Field Name="Audit Year">2010</Field> this line then it must say "Audit Year" is not present in the given XML.

below is the present XSD but it's not a proper way.


<<pre lang="xml">?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="temp" >
<xs:restriction base="xs:string">
  <xs:enumeration value="Audit Year" />
</xs:restriction>
<xs:element name="Metadata">
<xs:complexType>
  <xs:sequence>
    <xs:element maxOccurs="unbounded" name="Field">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
           <xs:attribute name="Name" type="temp" use="required"/>
           <!--<xs:attribute name="Name" type="xs:string" use="optional" />-->
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType></xs:element></xs:schema></pre>
Posted

In XSD, you can make some element optional or mandatory. This can be controlled by explicitly defining the multiplicity, which can be understood as a constraint over the number of occurrences, using the attributes minOccurs and maxOccurs. Here the usage is explained on a simple sample: http://stackoverflow.com/questions/9243772/how-to-make-an-element-in-xml-schema-optional[^].

See also: http://en.wikipedia.org/wiki/Multiplicity_%28software%29[^].

[EDIT]

Another thing to do is to declare polymorphic type Metadata (with polymorphic composition sequence).
See, for example:
http://ojitha.blogspot.com/2012/01/learn-xml-schema-by-example-2.html[^].

Alternatively, don't use sequence. Use a set of fixed components of different type with different multiplicity on each.

In other words, your schema is not adequate to the requirements on upper level. You need to redesign it.

—SA
 
Share this answer
 
v4
Comments
Member 11149146 11-Mar-15 9:57am    
@Sergey Alexandrovich Kryukov I understand that we can make some element optional or mandatory. But in my requirement I want the "element value" as mandatory that is "Audit Year" and remaining are optional that is "Claim Number" and "Auditor Name" respectively.
Sergey Alexandrovich Kryukov 11-Mar-15 10:23am    
The answer is: design proper XSD schema. It could not be the "Fields" elements of the same type. You contradict to yourself. You need to create a polymorphic parent element and use "type" attribute.
—SA
Sergey Alexandrovich Kryukov 11-Mar-15 10:26am    
Please see my update to the answer, after [EDIT].
—SA
Member 11149146 12-Mar-15 4:09am    
I appriciate your responce and time but sorry to say you I am not able to get it properly.As I am new in XML so I am facing little bit problem to create XSD as per my requirement with your suggestions. Please will you create XSD in favor of me So I will understand something. if it works, you've got bounty.
I found 2 links:
http://stackoverflow.com/questions/8925706/xml-schema-how-to-restrict-attribute-by-enumeration[^] - this one you have probably seen before, because you used similar approach
and
http://stackoverflow.com/questions/7690949/element-mandatory-attribute-declaration-in-xsd-schema[^] - this one needs your attention. Second answer contains 5 examples, where second one is probably that what you're looking for.

Note: 2 of 3 Field fields are integer base, but one is string base. It might be the reason of trouble!


[EDIT]
I'd try this one:
XML
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >

  <xs:element name="Metadata">
    <xs:complexType>
      <xs:sequence minOccurs="1" maxOccurs ="unbounded">
        <xs:element name="Field">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:integer">
                <xs:attribute name="Name" use="required">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:enumeration value="Audit Year"/>
                      <xs:enumeration value="Claim Number"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:attribute>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>


</xs:schema>


Note: I do not had enough time to test it.
 
Share this answer
 
v3
Comments
Member 11149146 12-Mar-15 4:13am    
Thanks for your responce.But these are related to element attribute optional or mandatory.And in my requirement I want the "Field attribute value" as mandatory that is "Audit Year" not Name and remaining are optional that is "Claim Number" and "Auditor Name" respectively. So with your links I can set Name as required but not "Audit Year".
Maciej Los 12-Mar-15 17:06pm    
See updated answer.
Another way is to define collection of fields and define which Field) field element is obligatory and which not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900