`
tongjian
  • 浏览: 45170 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

xml schema

阅读更多

A Simple XML Document

<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

 

<?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="note">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>
      </xs:schema>

 

The note element is a complex type because it contains other elements. The other elements (to, from, heading, body) are simple types because they do not contain other elements.

 

What is a Simple Element?

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

 

<xs:element name="color" type="xs:string" default="red"/>
<xs:element name="color" type="xs:string" fixed="red"/>

 

What is an Attribute?

Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type.

<xs:attribute name="xxx" type="yyy"/>
<xs:attribute name="lang" type="xs:string" use="required"/>

 

Restrictions on Values

<xs:element name="age">
<xs:simpleType>
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="120"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

Restrictions on a Set of Values

<xs:element name="car">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:enumeration value="Audi"/>
    <xs:enumeration value="Golf"/>
    <xs:enumeration value="BMW"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

 

Restrictions on a Series of Values

<xs:element name="letter">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-z]"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

Restrictions on Whitespace Characters

<xs:element name="address">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="preserve"/>  # replace |  collapse
  </xs:restriction>
</xs:simpleType>
</xs:element> 

The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters:

The whiteSpace constraint is set to "collapse", which means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space):

The whiteSpace constraint is set to "replace", which means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces:

Restrictions on Length

<xs:element name="password">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:length value="8"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

 

<xs:element name="password">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:minLength value="5"/>
    <xs:maxLength value="8"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

Restrictions for Datatypes

Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

 

 

What is a Complex Element?

A complex element is an XML element that contains other elements and/or attributes.

There are four kinds of complex elements:

  • empty elements
  • elements that contain only other elements
  • elements that contain only text
  • elements that contain both other elements and text

 

<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>
      <xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

Note that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared.

 

You can also base a complex element on an existing complex element and add some elements, like this:

<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
  <xs:complexContent>
    <xs:extension base="personinfo">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

 

Complex Empty Elements

<xs:element name="product">
  <xs:complexType>
    <xs:complexContent>
      <xs:restriction base="xs:integer">
        <xs:attribute name="prodid" type="xs:positiveInteger"/>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:element>

     xml:

<product prodid="1345" />

    or schema:

<xs:element name="product" type="prodtype"/>
<xs:complexType name="prodtype">
  <xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>

Complex Types Containing Elements Only

An "elements-only" complex type contains an element that contains only other elements.

<xs:element name="person" type="persontype"/>
<xs:complexType name="persontype">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

Complex Text-Only Elements

A complex text-only element can contain text and attributes.

<xs:element name="shoesize">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="country" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

xml:

<shoesize country="france">35</shoesize>

Complex Types with Mixed Content

A mixed complex type element can contain attributes, elements, and text.

<xs:element name="letter">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="orderid" type="xs:positiveInteger"/>
      <xs:element name="shipdate" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

xml:

<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>

Note: To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true".

 

Indicators

There are seven indicators:

Order indicators:

  • All
  • Choice
  • Sequence

Occurrence indicators:

  • maxOccurs
  • minOccurs

Group indicators:

  • Group name
  • attributeGroup name

All Indicator

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:

<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>

Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1 (the <minOccurs> and <maxOccurs> are described later).

Choice Indicator

The <choice> indicator specifies that either one child element or another can occur:

<xs:element name="person">
  <xs:complexType>
    <xs:choice>
      <xs:element name="employee" type="employee"/>
      <xs:element name="member" type="member"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

Sequence Indicator

The <sequence> indicator specifies that the child elements must appear in a specific order:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Occurrence Indicators

Occurrence indicators are used to define how often an element can occur.

Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.

maxOccurs Indicator

The <maxOccurs> indicator specifies the maximum number of times an element can occur:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.

minOccurs Indicator

The <minOccurs> indicator specifies the minimum number of times an element can occur:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string"
      maxOccurs="10" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.

Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:

A working example:

An XML file called "Myfamily.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
<person>
<full_name>Stale Refsnes</full_name>
</person>
</persons>

The XML file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements.

Here is the schema file "family.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="persons">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="person" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="full_name" type="xs:string"/>
            <xs:element name="child_name" type="xs:string"
            minOccurs="0" maxOccurs="5"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

 


Group Indicators

Group indicators are used to define related sets of elements.

Element Groups

Element groups are defined with the group declaration, like this:

<xs:group name="groupname">
  ...
</xs:group>

You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:

<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>

After you have defined a group, you can reference it in another definition, like this:

<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:group ref="persongroup"/>
    <xs:element name="country" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

Attribute Groups

Attribute groups are defined with the attributeGroup declaration, like this:

<xs:attributeGroup name="groupname">
  ...
</xs:attributeGroup>

The following example defines an attribute group named "personattrgroup":

<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

After you have defined an attribute group, you can reference it in another definition, like this:

<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
  <xs:complexType>
    <xs:attributeGroup ref="personattrgroup"/>
  </xs:complexType>
</xs:element>

The <any> Element

The <any> element enables us to extend the XML document with elements not specified by the schema!

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
      <xs:any minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

 

he <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

分享到:
评论

相关推荐

    XmlSchema-1.4.6.jar

    `XmlSchema-1.4.6.jar` 是一个包含Java绑定的XML Schema API的库,它为处理XML Schema文档提供了支持。在开发Web服务客户端时,尤其是与基于SOAP(简单对象访问协议)的Web服务交互时,这个库扮演了至关重要的角色。...

    根据xml schema生成xml

    &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;/xs:schema&gt; ``` 这个例子定义了一个`person`元素,包含`firstName`和`lastName`两个子元素,都是字符串类型。 2. **验证XML文档**:...

    XML与XMLSchema

    【XML与XMLSchema】 XML,全称可扩展标记语言(eXtensible Markup Language),是一种基于标准通用标记语言SGML的简化版本,由万维网联盟(W3C)于1996年开始制定,并在1998年2月10日发布了XML 1.0规范。它的设计...

    xmlschema-core-2.0.3的jar

    `xmlschema-core-2.0.3.jar` 是一个Java库,专门用于解析和操作XML Schema文档,它支持XML Schema 1.1标准。 在Spring Web服务中,XML Schema Core库扮演着关键角色。Spring Web Service框架是基于合同优先...

    XmlSchema-1.4.7.jar

    XmlSchema-1.4.7.jar 是一个Java库,它实现了W3C的XML Schema标准,用于处理XML Schema文档。这个版本(1.4.7)可能是该库的一个稳定版本,提供给用户免费使用。XML Schema是XML的一种规范,定义了如何验证XML文档的...

    XmlSchema-1.2.jar,xmlschema-1.2.jar,xml.jar

    XmlSchema-1.2.jar,xmlschema-1.2.jar,xml.jar

    理解XML Schema XML Schema进阶

    XML Schema是目前国际标准的XML建模工具,本文将对XML Schema进行详细的介绍,帮助读者初步掌握XML Schema的使用方法和XML Schema文档实例的具体语义。 1. 命名空间的使用 XML Schema中命名空间的使用是非常重要的...

    XML文件XML Schema.docXML Schema.doc

    "XML Schema知识点" XML Schema是XML语言为基础的,它用于可替代DTD。它用于描述XML文档的结构。XML Schema语言也被称为XML Schema Definition(XSD)。XML Schema的作用是定义一份XML文档的合法组件群,就像DTD的...

    XML Schema教程

    ### XML Schema 教程知识点详解 #### 一、XML Schema 概述 - **XML Schema 定义**:XML Schema 是一种基于 XML 的语言,用于描述 XML 文档的结构。它定义了 XML 文档中可以出现的元素、属性及其结构,并且指定了...

    西工大服务计算实验2-1_XML Schema

    XML Schema,全称为XML Schema Definition,是用于定义XML文档结构和数据类型的规范,它是W3C(World Wide Web Consortium)制定的一种标准。在本实验"西工大服务计算实验2-1_XML Schema"中,你的任务是编写一个程序...

    XMLSchema经典例题

    在“XMLSchema经典例题”中,我们可以深入探讨以下几个核心知识点: 1. **基本元素和类型定义**:XML Schema允许定义基本数据类型,如字符串、整数、浮点数等,并且可以自定义复杂的数据类型。例如,你可以创建一个...

    apache xmlschema api文档

    在描述中提到的"apache的xmlschema jar包的api文档"是开发者理解和使用这个API的关键资源。它通常包含详细的方法、类和接口的说明,以及如何使用它们的例子。这些文档能够帮助开发者了解如何在Java程序中导入和使用...

    XmlSchema-1.1.jar

    XmlSchema-1.1.jar XmlSchema-1.1.jar

    xmlschema实验及答案.doc

    XML Schema实验及答案 XML Schema是基于XML的_schema语言,是一种用于定义XML文档结构的语言。它是W3C制定的一个标准,用于描述XML文档的结构、约束和关系。XML Schema提供了一个强大的工具,用于定义和验证XML文档...

    xmlschema-core-2.1.0-API文档-中文版.zip

    赠送jar包:xmlschema-core-2.1.0.jar; 赠送原API文档:xmlschema-core-2.1.0-javadoc.jar; 赠送源代码:xmlschema-core-2.1.0-sources.jar; 赠送Maven依赖信息文件:xmlschema-core-2.1.0.pom; 包含翻译后的API...

    XmlSchema-1.4.7 jar包

    好不容易找到的XmlSchema-1.4.7 jar包,需要的同学速度下载吧。不黑心,只要1分~~~

    XMLSchema.chm

    这个“XMLSchema.chm”文件很可能是一个帮助文档,旨在为学习XML Schema的用户提供详细的指导和参考资料。下面我们将深入探讨XML Schema的相关知识点。 1. **XML Schema的作用**:XML Schema的主要功能是规范XML...

    AUTOSAR_MMOD_XMLSchema.zip

    标题中的"AUTOSAR_MMOD_XMLSchema.zip"指的是一个与AUTOSAR(AUTomotive Open System ARchitecture)相关的压缩包文件,它包含了MMOD(Model-based Modeling and Development)的XML Schema定义。AUTOSAR是一种全球...

    xmlschema-core-2.0.3.jar

    xmlschema-core-2.0.3.jar;xmlschema-core-2.0.3.jar;xmlschema-core-2.0.3.jar

Global site tag (gtag.js) - Google Analytics