Schema约束XML文件的实例

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

shiporder_1.xsd文件:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

      targetNamespace="http://www.itstar.cn/shiporder_1"

   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>

              <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>

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

<?xml version="1.0" encoding="UTF-8"?>

<xh:schema xmlns:xh="http://www.w3.org/2001/XMLSchema"

     elementFormDefault="qualified">

<!-- 简易元素的定义 -->

  <xh:element name="orderperson" type="xh:string" />

  <xh:element name="name" type="xh:string" />

  <xh:element name="address" type="xh:string" />

  <xh:element name="city" type="xh:string" />

  <xh:element name="country" type="xh:string" />

  <xh:element name="title" type="xh:string" />

  <xh:element name="note" type="xh:string" />

  <xh:element name="quantity" type="xh:positiveInteger" />

  <xh:element name="price" type="xh:decimal" />

<!-- 属性的定义 -->

  <xh:attribute name="orderid" type="xh:string" />

<!-- 复合元素的定义 -->

  <xh:element name="shipto">

    <xh:complexType>

      <xh:sequence>

        <xh:element ref="name" />

        <xh:element ref="address" />

        <xh:element ref="city" />

        <xh:element ref="country" />

      </xh:sequence>

     </xh:complexType>

   </xh:element>


  <xh:element name="item">

    <xh:complexType>

      <xh:sequence>

        <xh:element ref="title" />

        <xh:element ref="note" minOccurs="0" />

        <xh:element ref="quantity" />

        <xh:element ref="price" />

       </xh:sequence>

     </xh:complexType>

  </xh:element>


  <xh:element name="shiporder">

    <xh:complexType>

      <xh:sequence>

        <xh:element ref="orderperson" />

         <xh:element ref="shipto" />

        <xh:element ref="item" maxOccurs="unbounded" />

      </xh:sequence>

      <xh:attribute ref="orderid" use="required" />

    </xh:complexType>

  </xh:element>

</xh:schema>


shiporder.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<shiporder xmlns="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>


关于XML Schema的语法见: XML Schema官方教程(中文版):(下载地址:http://download.csdn.net/detail/xh16319/4587755)

你可能感兴趣的:(xml,schema,encoding)