I am having problem in writing a xsd file for validating an specific XML format. The XML format goes like this:
<?xml version="1.0" encoding="UTF-8"?>
<p:customers xmlns:p="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com NewXMLSchema.xsd ">
<p:customer>
<p:name salutation="Mr."/>
<p:age>25</p:age>
<p:discount>6</p:discount>
</p:customer>
<p:customer>
<p:name salutation="Ms."/>
<p:age>35</p:age>
<p:discount>10</p:discount>
</p:customer>
........
........
</p:customers>
The number of customer will vary; it may be one or more.
This is the XSD i came up with for validating this XML:
<xs:complexType name="customerName">
<xs:attribute name="salutation" type="xs:string" default="Mr."></xs:attribute>
</xs:complexType>
<xs:simpleType name="customerAge">
<xs:restriction base="xs:integer">
<xs:minInclusive value="18"></xs:minInclusive>
<xs:maxInclusive value="60"></xs:maxInclusive>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="customerdiscount">
<xs:restriction base="xs:integer">
<xs:minInclusive value="5"></xs:minInclusive>
<xs:maxInclusive value="30"></xs:maxInclusive>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="name" type="customerName"></xs:element>
<xs:element name="age" type="customerAge"></xs:element>
<xs:element name="discount" type="customerdiscount"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="customers">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="customer"></xs:element>
<xs:element name="customer" type="customer"></xs:element>
<xs:element name="customer" type="customer"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
In the above XSD, i am facing a few problems, need the resolutions:
1) I want the name to be something like this:
<name salutation="Mr.">XYZ NAme</name>
2) The customer names may be one or more. In the above code, i used sequence and hence there can be 3 and only 3 customer. This thing I need to rectify.
Please help me with these two stuff in XSD.
Regards,
I hope this helps:
1) You can extend xs:string as a complex type with an attribute:
2) You can use minOccurs and maxOccurs:
Also, I think you need to specify the schema’s target namespace at the top:
and prefix type references with p (because all types defined by the schema are in the target namespace):
With these changes, I could successfully validate the document above (with the dots removed).