Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsd choice for multiple elements

Tags:

I would like my Xml file to look like the following

<root> <name>a</name> <age>23</age> </root>   or   <root> <empno>b<empno> <designation>ase</designation> </root> 

is it possible to create a XML schema for the above using a "choice" indicator?something like below.

    <xsd:element name="root">     <xsd:complexType>     <xsd:choice>      <xsd:element name="name"/>     <xsd:element name="age" />       or      <xsd:element name="empno"/>     <xsd:element name="designation" />      <xsd:choice>      </xsd:complexType>     <xsd:element> 

Is it possible to do like this?

like image 803
Pradeep Achar Avatar asked Feb 26 '13 12:02

Pradeep Achar


People also ask

How do I make an element optional in XSD?

Using <xsd:choice> in an XSD The element in the root schema has to be optional. Add attribute minOccurs="0" on the element to make it optional.

What is XSD complex type?

Advertisements. Complex Element is an XML element which can contain other elements and/or attributes. We can create a complex element in two ways − Define a complex type and then create an element using the type attribute.

What is minOccurs and maxOccurs 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

Yes, You are almost there.. just missing a sequence .. Since it's the set of fields you have to wrap them under sequence..

These sequence tags will be under <Choice> tag. Now either of these set of tags (Sequence) will be validated.

<?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">   <xs:element name="root" type="root"/>   <xs:complexType name="root">     <xs:choice>       <xs:sequence>         <xs:element name="empno" type="xs:string" />         <xs:element name="designation" type="xs:string" />       </xs:sequence>       <xs:sequence>         <xs:element name="name" type="xs:string" />         <xs:element name="age" type="xs:unsignedByte" />       </xs:sequence>     </xs:choice>   </xs:complexType> </xs:schema> 

-----------------------------------------------------------------------------------------

I would like to add one more suggestion here:

I observe that you use nested declarations .. like this:

<?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">   <xs:element name="root">     <xs:complexType>       <xs:sequence>         <xs:element name="trunk">           <xs:complexType>             <xs:sequence>               <xs:element name="branch1" type="xs:string"/>               <xs:element name="branch2" type="xs:string"/>             </xs:sequence>           </xs:complexType>         </xs:element>         <xs:element name="other" type="xs:string" />       </xs:sequence>     </xs:complexType>   </xs:element> </xs:schema> 

Always prefer usage of Custom types! Like this:

<?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">   <xs:element name="root" type="root"/>    <xs:complexType name="root">     <xs:sequence>       <xs:element name="trunk" type="trunk"/>       <xs:element name="other" type="xs:string" />     </xs:sequence>   </xs:complexType>    <xs:complexType name="trunk">     <xs:sequence>       <xs:element name="branch1" type="xs:string"/>       <xs:element name="branch2" type="xs:string"/>     </xs:sequence>   </xs:complexType> </xs:schema> 

This improves readability and you can reuse the complexType/simpleType ..

hope it helps..

like image 151
InfantPro'Aravind' Avatar answered Oct 22 '22 12:10

InfantPro'Aravind'