XML学习之------>Schema约束XML文件的实例

shiporder_1.xsd文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.itstar.cn/shiporder_1"
  elementFormDefault="qualified">
  <xs:elementname="shiporder">
   <xs:complexType>
    <xs:sequence>
     <xs:elementname="orderperson"type="xs:string" />
     <xs:elementname="shipto">
      <xs:complexType>
                  <xs:sequence>
          <xs:elementname="name"type="xs:string"/>
          <xs:elementname="address"type="xs:string" />
          <xs:elementname="city"type="xs:string"/>
          <xs:elementname="country"type="xs:string" />
       </xs:sequence>
       </xs:complexType>
     </xs:element>
     <xs:elementname="item"maxOccurs="unbounded">
      <xs:complexType>
       <xs:sequence>
         <xs:elementname="title"type="xs:string"/>
        <xs:elementname="note"type="xs:string"minOccurs="0"/>
        <xs:elementname="quantity"type="xs:positiveInteger"></xs:element>
        <xs:elementname="price"type="xs:decimal"/>
       </xs:sequence>
      </xs:complexType>
     </xs:element>
    </xs:sequence>
    <xs:attributename="orderid"type="xs:string" use="required"/>
   </xs:complexType>
  </xs:element>
</xs:schema>


shiporder_2.xsd文件:(不知道为什么此种方式不能加入targetNamespace属性)

<?xmlversion="1.0"encoding="UTF-8"?>
<xh:schemaxmlns:xh="http://www.w3.org/2001/XMLSchema"
     elementFormDefault="qualified">
<!-- 简易元素的定义 -->
  <xh:elementname="orderperson"type="xh:string" />
  <xh:elementname="name"type="xh:string"/>
  <xh:elementname="address"type="xh:string" />
  <xh:elementname="city"type="xh:string"/>
  <xh:elementname="country"type="xh:string" />
  <xh:elementname="title"type="xh:string"/>
  <xh:elementname="note"type="xh:string"/>
  <xh:elementname="quantity"type="xh:positiveInteger"/>
  <xh:elementname="price"type="xh:decimal"/>
<!-- 属性的定义 -->
  <xh:attributename="orderid"type="xh:string" />
<!-- 复合元素的定义 -->
  <xh:elementname="shipto">
    <xh:complexType>
     <xh:sequence>
      <xh:elementref="name"/>
      <xh:elementref="address"/>
      <xh:elementref="city"/>
    <xh:elementref="country"/>
     </xh:sequence>
     </xh:complexType>
   </xh:element>

  <xh:elementname="item">
   <xh:complexType>
    <xh:sequence>
     <xh:elementref="title"/>
     <xh:elementref="note"minOccurs="0"/>
     <xh:elementref="quantity"/>
     <xh:elementref="price"/>
     </xh:sequence>
    </xh:complexType>
  </xh:element>

  <xh:elementname="shiporder">
   <xh:complexType>
    <xh:sequence>
      <xh:elementref="orderperson"/>
      <xh:elementref="shipto"/>
     <xh:elementref="item"maxOccurs="unbounded"/>
    </xh:sequence>
    <xh:attributeref="orderid"use="required" />
   </xh:complexType>
  </xh:element>
</xh:schema>



shiporder.xml文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<shiporderxmlns="http://www.itstar.cn/shiporder_1"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://www.itstar.cn/shiporder_1 shiporder_1.xsd"
                         orderid="111">
  <orderperson>orderperson</orderperson>
   <shipto>
   <name>name</name>
    <address>address</address>
    <city>city</city>
   <country>country</country>
  </shipto>
  <item>
    <title>title</title>
   <note>note</note>
    <quantity>100</quantity>
   <price>100.0</price>
  </item>
  <item>
   <title>title</title>
   <quantity>200</quantity>
   <price>80.0</price>
  </item>
</shiporder>


你可能感兴趣的:(schema)