I am facing problems in making a complex element, which allows both optional as well as mandatory elements. For the xml below, say h2 is mandatory, while h1 is optional, and the order does not matter.
Case 1:
<root>
<h1/>
<h2/>
</root>
Case 2:
<root>
<h2/>
</root>
Case 3:
<root>
<h2/>
<h1/>
</root>
XSD:
<xs:element name="root">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="h1" minOccurs="0"></xs:element>
<xs:element name="h2" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
the third case above fails in this xsd, but such case is valid. I need an xsd that is valid for all the above mentioned cases.
Knowing that what you want is:
to make h2 occur atmost 1, while h1 can occur as many times as possible
You could use this XSD in which you are defining that a XML would be valid if its content is something like (RegExpr) <root><h1/>*<h2/><h1/>*</root>
.
XSD:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="1">
<xsd:element name="h1" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="h2" minOccurs="1" maxOccurs="1"/>
<xsd:element name="h1" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<root> <h1/> <h1/> <h2/> <h1/> </root>
2)
<root> <h1/> <h2/> </root>
<h2/>
Elements.
<root> <h2/> <h1/> <h2/> </root>
No <h2/>
Element.
<root> <h1/> <h1/> </root>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.