我正试图为我的xml定义一些xsd文档。
但我是新手,经过一番尝试后感到困惑。
我的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>
正如您所看到的,有CCD_ 1和CCD_。
1-<auth>
是必需的-><code>
也是必需的,并且代码是32长度的字符串。
2-<create>
可替换为<update>
或<delete>
3-CCD_ 8可重复2-4次。
4-<contact>
必须以精确的属性重复4次。
这是我的尝试,但有很多漏洞。
<?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>
我已经根据您的需求创建了XSD。我已经使用这个在线验证器来根据模式验证xml。干杯
<?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>