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语言之前,你应该对XML和XML的命名空间有基本的了解。那样会帮助你了解DTD(Document 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:]
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 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:
[对数据类型提供的支持有:]
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书写的好处有:]
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”,在某些国家会被解释为11月3日,而在另一些国家却被解释成3月11日],而相同的数据在XML上表述成:
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:
[可扩展性的定义带来的好处有:]
Well-Formed is not Enough
[良好格式还并不足够]
A well-formed XML document is a document that conforms to the XML syntax rules:
[一个具有良好格式的XML文档是完全符合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文档]
Don't forget me this weekend!
|
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"中的元素:]
|
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".而第二行到第五行分别定义了to和from等元素为"#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"中的元素:]
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">
|
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:]
"http://www.w3schools.com/dtd/note.dtd"> Don't forget me this weekend!
|
A Reference to an XML Schema
[XML Schema的引用]
This XML document has a reference to an XML Schema:
[这个XML文档引用自一个XML Schema文档:]
xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd">
Don't forget me this weekend!
|
XSD - The
[XSD
The
[
The
[
The
[[
... ...
|
The
[[
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> ... ...
|
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的引用:]
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"> Don't forget me this weekend!
|
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" |