Schema——demo

schema.xsd myXml.xml
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
     targetNamespace="meihao"     //这里必须有
     elementFormDefault="qualified">
<xs:element name="shiporder">
<xs:complexType>
  <xs:sequence>
   <xs:element name="orderperson" type="xs:string"/>
   <xs:element name="shipto">
    <xs:complexType>
     <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="address" type="xs:string"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="country" type="xs:string"/>
     </xs:sequence>
    </xs:complexType>
   </xs:element>
   <xs:element name="item" maxOccurs="unbounded">
    <xs:complexType>
     <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string" minOccurs="0"/>
      <xs:element name="quantity" type="xs:positiveInteger"/>
      <xs:element name="price" type="xs:decimal"/>
     </xs:sequence>
    </xs:complexType>
   </xs:element>
  </xs:sequence>
  <xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
< shiporder orderid="No.1" xmlns="meihao"   //这里随便写,xmlns后面可以像xsd里面一样写一个命名:xmlns:mh
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   //固定;不要也可以,不要的话结果显示就没有这行
     xsi:schemaLocation="meihao schema.xsd">  //xsd文件里的空格加上文件名字
        <orderperson >名单</orderperson>
        <shipto>
        <name>姓名</name>
                <address>地址</address>
                <city>城市</city>
                <country>乡村</country>
        </shipto>
        <item>
                <title>标题</title>
                <note>注释</note>
                <quantity>3</quantity>
                <price>34</price>
        </item>

</shiporder>


注意: 书写时候有意的空格分层对程序没有什么影响;闭合标签要成对的写出来,不然出错的时候只会随便显示一行文字

//结果截图

猜你喜欢

转载自www.cnblogs.com/meihao1203/p/9181685.html