Reputation: 1558
I'm trying to define some xsd document for my xml.
but I'm new in that and confused after some try.
my xml:
<?xml version="1.0" encoding="utf-8"?>
<mashhadhost xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com mhost.xsd">
<create>
<name>example.ir</name>
<period>60</period>
<ns>
<hostAttr>
<hostName>ns1.example.ir</hostName>
<hostAddr ip="v4">192.0.2.2</hostAddr>
</hostAttr>
</ns>
<contact type="holder">ex61-irnic</contact>
<contact type="admin">ex61-irnic</contact>
<contact type="tech">ex61-irnic</contact>
<contact type="bill">ex61-irnic</contact>
</create>
<auth>
<code>TOKEN</code>
</auth>
</mashhadhost>
as you can see there are <create>
and <auth>
childs.
1- <auth>
is required -> <code>
is required too and code is 32 length string.
2- <create>
can be replace with <update>
or <delete>
3- <hostAttr>
can repeat between 2-4 times.
4- <contact>
have to repeat 4 times with exact attributes.
it's my try but there are a lot of hole in that.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="mashhadhost">
<xs:complexType>
<xs:sequence>
<xs:element name="create">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="period" type="xs:string"/>
<xs:element name="ns"/>
<xs:element name="contact" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="auth">
<xs:complexType>
<xs:sequence>
<xs:element name="code" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Upvotes: 0
Views: 69
Reputation: 14677
I've created the XSD as per your requirement. I've used this online validator to validate the xml against the schema. Cheers!!!
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="mashhadhost" targetNamespace="http://www.w3schools.com" xmlns:mstns="http://www.w3schools.com" xmlns="http://www.w3schools.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:complexType name="bodyType" mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" />
<xs:element name="period" type="xs:string" minOccurs="0" />
<xs:element name="ns" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="hostAttr" minOccurs="2" maxOccurs="4">
<xs:complexType>
<xs:sequence>
<xs:element name="hostName" type="xs:string" minOccurs="1" />
<xs:element name="hostAddr" nillable="true" minOccurs="1">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="hostAddr_Text" msdata:Ordinal="1">
<xs:extension base="xs:string">
<xs:attribute name="ip" form="unqualified" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="contact" minOccurs="4" maxOccurs="4">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="contact_Text" msdata:Ordinal="1">
<xs:extension base="xs:string">
<xs:attribute name="type" form="unqualified" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="mashhadhost">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="create" type="bodyType"/>
<xs:element name="udpate" type="bodyType"/>
<xs:element name="delete" type="bodyType"/>
<xs:element name="auth">
<xs:complexType>
<xs:sequence>
<xs:element name="code" minOccurs="0" >
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="32"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Upvotes: 1