XML Schema学习教程(一)-XML Schema介绍

来源:互联网 发布:量男朋友的丁丁知乎 编辑:程序博客网 时间:2024/05/21 06:20

Introduction to XML Schema
[XML Schema
介绍]

翻译:linqingfeng
英语原文: http://www.w3schools.com/schema/default.asp


XML Schema is an XML based alternative to DTD.
[XML Schema
是一种XML文件类型定义的基本方法]

An XML schema describes the structure of an XML document.
[
每个XML Schema描述XML文档的结构]

The XML Schema language is also referred to as XML Schema Definition (XSD).
[XML Schema
也称为XML模式定义(XSD)]


What You Should Already Know
[
在学习之前你应该知道什么]

Before you study the XML Schema Language, you should have a basic understanding of XML and XML Namespaces. It will also help to have some basic understanding of DTD.
[
在你开始学习XML Schema语言之前,你应该对XMLXML的命名空间有基本的了解。那样会帮助你了解DTDDocument Type Definition文件类型定义)].


What is an XML Schema?
[XML Schema
是什么?]

The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.
[XML Schema
目标是定义合法的XML文档,就像DTD那样]

An XML Schema:
[
任一个XML Schema]

  • defines elements that can appear in a document
    [
    定义在文档中的元素]
  • defines attributes that can appear in a document
    [
    定义在文档中的属性]
  • defines which elements are child elements
    [
    定义哪些元素为子元素]
  • defines the order of child elements
    [
    定义子元素的阶]
  • defines the number of child elements
    [
    定义子元素的数]
  • defines whether an element is empty or can include text
    [
    定义元素是否为空]
  • defines data types for elements and attributes
    [
    定义元素和属性的数据类型]
  • defines default and fixed values for elements and attributes
    [
    定义元素和属性的默认值和固定值]

XML Schemas are the Successors of DTDs
[XML Schemas
DTD的替代者]

We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs. Here are some reasons:
[
我们认为很快XML Schemas就会替代DTD而大量应用于Web应用。原因有如下几个:]

  • XML Schemas are extensible to future additions
    [XML Schemas
    易于未来的扩展]
  • XML Schemas are richer and more useful than DTDs
    [XML Schemas
    DTD更有用处]
  • XML Schemas are written in XML
    [XML Schemas
    本身就是XML]
  • XML Schemas support data types
    [XML Schemas
    支持数据类型]
  • XML Schemas support namespaces
    [XML Schemas
    支持命名空间]

XML Schema is a W3C Recommendation
[XML Schemas
W3C推荐使用的]

XML Schema was originally proposed by Microsoft, but became an official W3C recommendation in May 2001.
[XML Schemas
最初由微软提出,并在2001年五月成为W3C的官方指定标准]

The specification is now stable and has been reviewed by the W3C Membership.
[
其规格文档现已稳定并加入到W3C标准中]

XML Schemas - Why?
[
为什么选用XML Schemas]

 


There are a number of reasons why XML Schema is better than DTD.
[XML Schemas
有趣多方面优于DTD]


XML Schema has Support for Data Types
[XML Schemas
支持数据类型]

One of the greatest strengths of XML Schemas is the support for data types.
[XML Schemas
其中最好的方面之一就是支持数据类型]

With the support for data types:
[
对数据类型提供的支持有:]

  • It is easier to describe permissible document content
    [
    易于描述文档内容是否允许]
  • It is easier to validate the correctness of data
    [
    易于验证数据的正确性]
  • It is easier to work with data from a database
    [
    易于复合数据库操作数据]
  • It is easier to define data facets (restrictions on data)
    [
    易于定义数据约束]
  • It is easier to define data patterns (data formats)
    [
    易于格式化数据]
  • It is easier to convert data between different data types
    [
    易于数据在不同的数据类型中转换]

XML Schemas use XML Syntax
[XML Schemas
是用XML的语法]

Another great strength about XML Schemas is that they are written in XML.
[
另一个最大的优点是XML Schemas是用XML书写的]

Because XML Schemas are written in XML:
[XML Schemas
XML书写的好处有:]

  • You don't have to learn another language
    [
    不用学习另一种新的语言]
  • You can use your XML editor to edit your Schema files
    [
    能在XML的编辑器中编写XML Schemas文件]
  • You can use your XML parser to parse your Schema files
    [
    能用XML解释器去解释XML Schemas文件]
  • You can manipulate your Schema with the XML DOM
    [
    能用XML DOM(Document Object Model,文档物件模型)操作XML Schemas文件]
  • You can transform your Schema with XSLT
    [
    能用XSLT来转换XML Schemas文件]

XML Schemas Secure Data Communication
[XML Schemas
数据通信更安全]

When data is sent from a sender to a receiver it is essential that both parts have the same "expectations" about the content.
[
当数据从发送方传递给接受方时, XML Schemas会让显示的内容达到你的期望”]

With XML Schemas, the sender can describe the data in a way that the receiver will understand.
[XML Schemas
文件能让发送方的数据被接受方所解释]

A date like this: "03-11-2004" will, in some countries, be interpreted as 3. November and in other countries as 11. March, but an XML element with a data type like this:
[
例如一个日期的表述” 03-11-2004”,在某些国家会被解释为113,而在另一些国家却被解释成311],而相同的数据在XML上表述成:

<date type="date">2004-03-11</date>

ensures a mutual understanding of the content because the XML data type date requires the format YYYY-MM-DD.
[
由于XML数据类型定义了他的格式是YYYY-MM-DD,从而使能正确的表述其内容的含义]


XML Schemas are Extensible
[XML Schemas
是可扩展的]

XML Schemas are extensible, just like XML, because they are written in XML.
[XML Schemas
是可扩展的,就像普通的XML一样,因为他本身就是一个XML]

With an extensible Schema definition you can:
[
可扩展性的定义带来的好处有:]

  • Reuse your Schema in other Schemas
    [
    可从用你的规则与其他规则]
  • Create your own data types derived from standard types
    [
    从其他的标准的数据类型中创建自定义的数据类型]
  • Reference multiple schemas from the same document
    [
    同一个文档可以引用多个规则]

Well-Formed is not Enough
[
良好格式还并不足够]

A well-formed XML document is a document that conforms to the XML syntax rules:
[
一个具有良好格式的XML文档是完全符合XML的语法规则的:]

  • must begin with the XML declaration
    [
    必须由XML声明开始]
  • must have one unique root element
    [
    必须有且仅有一个根节点]
  • all start tags must match end-tags
    [
    所有标签都必须有相对的结束标签]
  • XML tags are case sensitive
    [XML
    的标签是区分大小写的]
  • all elements must be closed
    [
    所有的元素都必须是闭合的]
  • all elements must be properly nested
    [
    所有元素都必须合理的嵌套(元素不可以交叉)]
  • all attribute values must be quoted
    [
    所有的属性都必须被引用]
  • XML entities must be used for special characters
    [
    所有的XML实体都必须用特殊的字符来做]

Even if documents are Well-Formed they can still contain errors, and those errors can have serious consequences. Think of this situation: you order 5 gross of laser printers, instead of 5 laser printers. With XML Schemas, most of these errors can be caught by your validating software.
[
即使文档完全符合格式也会包含错误,而且有些错误会有严重的逻辑问题.想一想这样的情况:你订购五台激光打印机是为了来替代五台激光打印机.配合XML Schemas,大部分的错误都可以由你的软件校验出来的.]

XSD How To
[XSD
如何]


XML documents can have a reference to a DTD or an XML Schema.
[XML
文档能和一个DTD或者一个XML Schema相关]


A Simple XML Document
[
一个简单的XML文档]

Look at this simple XML document called "note.xml":
[
看看这个名为"note.xml"XML文档]

<?xml version="1.0"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

 


A Simple DTD
[
一个简单的DTD]

This is a simple DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):
[
这个简单的DTD"note.dtd"定义了"note.xml"中的元素:]

<!ELEMENT note (to, from, heading, body)>

<!ELEMENT to (#PCDATA)>

<!ELEMENT from (#PCDATA)>

<!ELEMENT heading (#PCDATA)>

<!ELEMENT body (#PCDATA)>

Line 1 defines the note element to have four elements: "to, from, heading, body". Line 2-5 defines the to element to be of the type "#PCDATA", the from element to be of the type "#PCDATA", and so on...
[
第一行定义了note元素有四个子元素: "to, from, heading, body".而第二行到第五行分别定义了tofrom等元素为"#PCDATA"]


A Simple XML Schema
[
一个简单的XML Schema]

This is a simple XML Schema file called "note.xsd" that defines the elements of the XML document above ("note.xml"):
[
这个简单的XML Schema文件"note.xsd"同样定义了"note.xml"中的元素:]

<?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 said to be of a complex type because it contains other elements. The other elements (to, from, heading, body) are said to be simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.
[
那个note元素由于包含有其他的子元素而被定义为complex type(复合类型). 其他包含在它里面的元素(to, from, heading, body)都被定义为simple types(简单类型). 你将会在以后的章节里面学到更多关于复合类型和简单类型的知识.]


A Reference to a DTD
[DTD
的引用]

This XML document has a reference to a DTD:
[
这个XML文档引用自一个DTD:]

<?xml version="1.0"?>

<!DOCTYPE note SYSTEM

"http://www.w3schools.com/dtd/note.dtd">

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

 


A Reference to an XML Schema
[XML Schema
的引用]

This XML document has a reference to an XML Schema:
[
这个XML文档引用自一个XML Schema文档:]

<?xml version="1.0"?>

<note

xmlns="http://www.w3schools.com"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.w3schools.com note.xsd">

 

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

 

XSD - The <schema> Element
[XSD
中的<schema>元素]


The <schema> element is the root element of every XML Schema!
[<schema>
元素是每一个XML Schema文件的根元素!]


The <schema> Element
[<schema>
元素]

The <schema> element is the root element of every XML Schema:
[[<schema>
元素是每一个XML Schema文件的根元素:]

<?xml version="1.0"?>

<xs:schema>

...

...

</xs:schema>

The <schema> element may contain some attributes. A schema declaration often looks something like this:
[[<schema>
元素包含一些属性.一般声明如下:]

<?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:schema>

The following fragment:
[
以下片段:]

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

indicates that the elements and data types used in the schema (schema, element, complexType, sequence, string, boolean, etc.) come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:
[
说明元素和数据类型的使用模式(schema, element, complexType, sequence, string, Boolean等等)来自于"http://www.w3.org/2001/XMLSchema"命名空间.同时也可用xs:作为前缀]

This fragment:
[
如下片段:]

targetNamespace="http://www.w3schools.com"

indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.
[
说明元素(note, to, from, heading, body.)的使用模式来自于命名空间"http://www.w3schools.com"]

This fragment:
[
如下片段:]

xmlns="http://www.w3schools.com"

indicates that the default namespace is "http://www.w3schools.com".
[
说明默认的命名空间是"http://www.w3schools.com".]

This fragment:
[
如下片段:]

elementFormDefault="qualified"

indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
[
说明所有全局元素的子元素将被以缺省方式放到目标命名空间,连同全局元素或者类型一起]


Referencing a Schema in an XML Document
[
XML文件中引用Schema]

This XML document has a reference to an XML Schema:
[
如下XML文档有一个XML Schema的引用:]

<?xml version="1.0"?>

<note xmlns="http://www.w3schools.com"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.w3schools.com note.xsd">

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

The following fragment:
[
下面片段:]

xmlns="http://www.w3schools.com"

specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this XML document are declared in the "http://www.w3schools.com" namespace.
[
声明默认的命名空间.此声明告知模式校验器让所有XML元素都在命名空间"http://www.w3schools.com"]

Once you have the XML Schema Instance namespace available:
[
有时你可以用到XML模式实例命名空间:]

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

you can use the schemaLocation attribute. This attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace:
[
你能用上schemaLocation这个属性.这个属性有两个含义.第一个含义是这个命名空间被使用.第二个含义是定位XML schema用到的命名空间:]

xsi:schemaLocation="http://www.w3schools.com note.xsd"

原创粉丝点击