如何创建一份XMLSchema And XML ELements

Schema系列文章

1、XML对Schema文档的引用123

2、Schema文档概述123

3、Schema之简单元素、复合元素和属性

4、Schema约束

5、Schema指示器

下面都是根据这个XML文件来写Schema:

<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="shiporder.xsd">
 <orderperson>John Smith</orderperson>
 <shipto>
  <name>Ola Nordmann</name>
  <address>Langgt 23</address>

  <city>4000 Stavanger</city>
  <country>Norway</country>
 </shipto>
 <item>
  <title>Empire Burlesque</title>

  <note>Special Edition</note>
  <quantity>1</quantity>
  <price>10.90</price>
 </item>

 <item>
  <title>Hide your heart</title>
  <quantity>1</quantity>
  <price>9.90</price>

 </item>
</shiporder>

方式一:常规方法:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<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>

方式二:先定义所有的元素和属性,然后用ref属性引用它们:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<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:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>

<xs:element name="price" type="xs:decimal"/>
<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element name="shipto">
 <xs:complexType>
  <xs:sequence>
   <xs:element ref="name"/>

   <xs:element ref="address"/>
   <xs:element ref="city"/>
   <xs:element ref="country"/>
  </xs:sequence>

 </xs:complexType>
</xs:element>
<xs:element name="item">
 <xs:complexType>
  <xs:sequence>
   <xs:element ref="title"/>

   <xs:element ref="note" minOccurs="0"/>
   <xs:element ref="quantity"/>
   <xs:element ref="price"/>

  </xs:sequence>
 </xs:complexType>
</xs:element>
<xs:element name="shiporder">
 <xs:complexType>
  <xs:sequence>

   <xs:element ref="orderperson"/>
   <xs:element ref="shipto"/>
   <xs:element ref="item" maxOccurs="unbounded"/>

  </xs:sequence>
  <xs:attribute ref="orderid" use="required"/>
 </xs:complexType>
</xs:element>
</xs:schema>

方式三:定义种类或类型,这使我们能重新用元素定义。通过给简单类型和复合类型元素命名,接着在元素的种类属性类型里指明它们的方法:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">

 <xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
 <xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
 <xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
 <xs:restriction base="xs:string">

  <xs:pattern value="[0-9]{6}"/>
 </xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
 <xs:sequence>

  <xs:element name="name" type="stringtype"/>
  <xs:element name="address" type="stringtype"/>
  <xs:element name="city" type="stringtype"/>

  <xs:element name="country" type="stringtype"/>
 </xs:sequence>
</xs:complexType>
<xs:complexType name="itemtype">

 <xs:sequence>
  <xs:element name="title" type="stringtype"/>
  <xs:element name="note" type="stringtype" minOccurs="0"/>

  <xs:element name="quantity" type="inttype"/>
  <xs:element name="price" type="dectype"/>
 </xs:sequence>

</xs:complexType>
<xs:complexType name="shipordertype">
 <xs:sequence>
  <xs:element name="orderperson" type="stringtype"/>

  <xs:element name="shipto" type="shiptotype"/>
  <xs:element name="item" maxOccurs="unbounded" type="itemtype"/>

 </xs:sequence>
 <xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>

XSD Elements
XSD元素

Element
元素
Explanation
解释
all Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time
指明了子元素可以以任意顺序出现,每个子元素出现0到1次
annotation Specifies the top-level element for schema comments
为schema注释指定了最高阶层的元素
any Enables the author to extend the XML document with elements not specified by the schema
让编者能够用没有被schema指定的元素来扩展XML文件
anyAttribute Enables the author to extend the XML document with attributes not specified by the schema
让编者能够用没有被schema指定的属性来扩展XML文件
appInfo Specifies information to be used by the application (must go inside annotation)
指定了将被应用程序所用到的信息(必须在annotation里)
attribute Defines an attribute
定义属性
attributeGroup Defines an attribute group to be used in complex type definitions
定义了用于复合种类定义的属性组
choice Allows only one of the elements contained in the <choice> declaration to be present within the containing element
只允许<choice>声明里的元素里的一个元素出现在所含的元素中。
complexContent Defines extensions or restrictions on a complex type that contains mixed content or elements only
在包含混合内容和元素的复合类型(complex type)上定义扩展或限制
complexType Defines a complex type element
定义了复合类型元素(complex type element)
documentation Defines text comments in a schema (must go inside annotation)
定义了schema里的文本注释(必须在annotation里)
element Defines an element
定义元素
extension Extends an existing simpleType or complexType element
扩展一个现存的简单类型或复合类型元素
field Specifies an XPath expression that specifies the value used to define an identity constraint
指明一个XPath表达式,以指明用于定义身份约束的值
group Defines a group of elements to be used in complex type definitions
定义了将用于复合类型说明的一组元素
import Adds multiple schemas with different target namespace to a document
用不同的目标名称空间把多项schema添加到一份文件
include Adds multiple schemas with the same target namespace to a document
用同一目标名称空间把多项schema添加到文件
key Specifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document
在实例文档包含的元素里指明某属性或元素值,使它们成为“钥匙”(特殊的,非零的,经常是存在的)
keyref Specifies that an attribute or element value correspond to those of the specified key or unique element
定义用于回应已指明关键或特殊元素的属性或元素值
list Defines a simple type element as a list of values
一张值的列表的形式定义一个简单类型元素
notation Describes the format of non-XML data within an XML document
描述在XML文档里的非XML形式数据的格式
redefine Redefines simple and complex types, groups, and attribute groups from an external schema
从外部的schema中重新定义简单和复合类型,群,以及属性群
restriction Defines restrictions on a simpleType, simpleContent, or a complexContent
在简单类型,简单文本或复合文本上定义约束
schema Defines the root element of a schema
定义schema的根元素
selector Specifies an XPath expression that selects a set of elements for an identity constraint
指明一段可为身份约束选择一组节点的XPath表达
sequence Specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times
指明了子元素必须以一定顺序出现。每个子元素可以出现任意次
simpleContent Contains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements
包含着作为只有文本的复合类型或简单类型的文本的扩展和约束,不包含元素
simpleType Defines a simple type and specifies the constraints and information about the values of attributes or text-only elements
定义了简单类型,指定了关于属性值或只有文本的元素的信息
union Defines a simple type as a collection (union) of values from specified simple data types
从已指定的简单数据类型里定义简单种类,作为值的集合
unique Defines that an element or an attribute value must be unique within the scope
定义了在所属范围内一个元素或属性值必须是独特(没有重复的)

更多参考http://www.cnblogs.com/caoxch/archive/2006/11/17/563856.html

原文地址:https://www.cnblogs.com/LittleFeiHu/p/1895084.html