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:]
- 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”,在某些國家會被解釋為11月3日,而在另一些國家卻被解釋成3月11日],而相同的數據在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".而第二行到第五行分別定義了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"中的元素:]
<?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等等)來自于"命名空間.同時也可用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"
|
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=705007