XML Schema 是DTD的替代者。 XML Schema 用於描述XML 文檔的結構。XML Schema 語言也稱作XML Schema 定義(XML Schema Definition,XSD)。
XML Schema:
• 定義可出現在文檔中的元素
• 可更容易地描述允許的文檔內容
• 可更容易地驗證數據的正確性
• 可更容易地與來自數據庫的數據一併工作
• 可更容易地定義數據約束(data facets)
• 可更容易地定義數據模型(或稱數據格式)
• 可更容易地在不同的數據類型間轉換數據
注:數據約束,或稱facets,是XML Schema 原型中的一個術語,中文可譯爲“面”,用來約束數據類型的容許值。
另一個關於 XML Schema 的重要特性是,它們由XML 編寫。
由 XML 編寫XML Schema 有很多好處:
• 不必學習新的語言
• 可使用 XML 編輯器來編輯Schema 文件
• 可使用 XML 解析器來解析Schema 文件
• 可通過 XML DOM 來處理Schema
• 可通過 XSLT 來轉換Schema
note.xml
<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
note.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
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>
在xml中引用note.xsd
<?xml version="1.0"?>
<note xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn note.xsd">
spring-beans-4.0部分代碼
<xsd:element name="beans"><xsd:annotation><xsd:documentation>
Container for <bean> and other elements, typically the root element in the document.
Allows the definition of default values for all nested bean definitions. May itself
be nested for the purpose of defining a subset of beans with certain default values or
to be registered only when certain profile(s) are active. Any such nested <beans> element
must be declared as the last element in the document.
</xsd:documentation></xsd:annotation><xsd:complexType><xsd:sequence><xsd:element ref="description" minOccurs="0"/><xsd:choice minOccurs="0" maxOccurs="unbounded"><xsd:element ref="import"/><xsd:element ref="alias"/><xsd:element ref="bean"/><xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/></xsd:choice><xsd:element ref="beans" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence><xsd:attribute name="profile" use="optional" type="xsd:string"><xsd:annotation><xsd:documentation>
The set of profiles for which this <beans> element should be parsed. Multiple profiles
can be separated by spaces, commas, or semi-colons.
If one or more of the specified profiles are active at time of parsing, the <beans>
element will be parsed, and all of its <bean> elements registered, <import>
elements followed, etc. If none of the specified profiles are active at time of
parsing, then the entire element and its contents will be ignored.
If a profile is prefixed with the NOT operator '!', e.g.
<beans profile="p1,!p2">
indicates that the <beans> element should be parsed if profile "p1" is active or
if profile "p2" is not active.
Profiles are activated in one of two ways:
Programmatic:
ConfigurableEnvironment#setActiveProfiles(String...)
ConfigurableEnvironment#setDefaultProfiles(String...)
Properties (typically through -D system properties, environment variables, or
servlet context init params):
spring.profiles.active=p1,p2
spring.profiles.default=p1,p2
</xsd:documentation></xsd:annotation></xsd:attribute><xsd:attribute name="default-lazy-init" default="default" type="defaultable-boolean"><xsd:annotation><xsd:documentation>
The default 'lazy-init' value; see the documentation for the
'lazy-init' attribute of the 'bean' element.
</xsd:documentation></xsd:annotation></xsd:attribute><xsd:attribute name="default-merge" default="default" type="defaultable-boolean"><xsd:annotation><xsd:documentation>
The default 'merge' value; see the documentation for the
'merge' attribute of the various collection elements. The default
is 'false'.
</xsd:documentation></xsd:annotation></xsd:attribute><xsd:attribute name="default-autowire" default="default"><xsd:annotation><xsd:documentation>
The default 'autowire' value; see the documentation for the
'autowire' attribute of the 'bean' element. The default is 'default'.
</xsd:documentation></xsd:annotation><xsd:simpleType><xsd:restriction base="xsd:NMTOKEN"><xsd:enumeration value="default"/><xsd:enumeration value="no"/><xsd:enumeration value="byName"/><xsd:enumeration value="byType"/><xsd:enumeration value="constructor"/></xsd:restriction></xsd:simpleType></xsd:attribute><xsd:attribute name="default-autowire-candidates" type="xsd:string"><xsd:annotation><xsd:documentation>
A default bean name pattern for identifying autowire candidates:
e.g. "*Service", "data*", "*Service*", "data*Service".
Also accepts a comma-separated list of patterns: e.g. "*Service,*Dao".
See the documentation for the 'autowire-candidate' attribute of the
'bean' element for the semantic details of autowire candidate beans.
</xsd:documentation></xsd:annotation></xsd:attribute><xsd:attribute name="default-init-method" type="xsd:string"><xsd:annotation><xsd:documentation>
The default 'init-method' value; see the documentation for the
'init-method' attribute of the 'bean' element.
</xsd:documentation></xsd:annotation></xsd:attribute><xsd:attribute name="default-destroy-method" type="xsd:string"><xsd:annotation><xsd:documentation>
The default 'destroy-method' value; see the documentation for the
'destroy-method' attribute of the 'bean' element.
</xsd:documentation></xsd:annotation></xsd:attribute><xsd:anyAttribute namespace="##other" processContents="lax"/></xsd:complexType></xsd:element>