Schema入门之XMLSchema使用及元素
来源:互联网 发布:智能网络应用前景 编辑:程序博客网 时间:2024/05/17 01:56
3.如何使用 XSD
========================================
XML 文档可对 DTD 或 XML Schema 进行引用。
一个简单的 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>DTD 文件
下面这个例子是名为 "note.dtd" 的 DTD 文件,它对上面那个 XML 文档的元素进行了定义:
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>第 1 行定义 note 元素有四个子元素:"to, from, heading, body"。
第 2-5 行定义了 to, from, heading, body 元素的类型是 "#PCDATA"。
XML Schema
下面这个例子是一个名为 "note.xsd" 的 XML Schema 文件,它定义了上面那个 XML 文档的元素:
<?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>note 元素是一个复合类型,因为它包含其他的子元素。其他元素 (to, from, heading, body) 是简易类型,因为它们没有包含其他元素。您将在下面的章节学习更多有关复合类型和简易类型的知识。
对 DTD 的引用
此文件包含对 DTD 的引用:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"http://www.w3school.com.cn/dtd/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>对 XML Schema 的引用
此文件包含对 XML Schema 的引用:
<?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">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
========================================
4.XSD - <schema> 元素
<schema> 元素是每一个 XML Schema 的根元素。
<schema> 元素
<schema> 元素是每一个 XML Schema 的根元素:
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema><schema> 元素可包含属性。一个 schema 声明往往看上去类似这样:
<?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:schema>代码解释:
下面的片断:
xmlns:xs="http://www.w3.org/2001/XMLSchema"显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:
这个片断:
targetNamespace="http://www.w3school.com.cn" 显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。
这个片断:
xmlns="http://www.w3school.com.cn" 指出默认的命名空间是 "http://www.w3school.com.cn"。
这个片断:
elementFormDefault="qualified" 指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。
在 XML 文档中引用 Schema
此 XML 文档含有对 XML Schema 的引用:
<?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">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>代码解释:
下面的片断:
xmlns="http://www.w3school.com.cn" 规定了默认命名空间的声明。此声明会告知 schema 验证器,在此 XML 文档中使用的所有元素都被声明于 "http://www.w3school.com.cn" 这个命名空间。
一旦您拥有了可用的 XML Schema 实例命名空间:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 您就可以使用 schemaLocation 属性了。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:
xsi:schemaLocation="http://www.w3school.com.cn note.xsd"
========================================
XML 文档可对 DTD 或 XML Schema 进行引用。
一个简单的 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>DTD 文件
下面这个例子是名为 "note.dtd" 的 DTD 文件,它对上面那个 XML 文档的元素进行了定义:
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>第 1 行定义 note 元素有四个子元素:"to, from, heading, body"。
第 2-5 行定义了 to, from, heading, body 元素的类型是 "#PCDATA"。
XML Schema
下面这个例子是一个名为 "note.xsd" 的 XML Schema 文件,它定义了上面那个 XML 文档的元素:
<?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>note 元素是一个复合类型,因为它包含其他的子元素。其他元素 (to, from, heading, body) 是简易类型,因为它们没有包含其他元素。您将在下面的章节学习更多有关复合类型和简易类型的知识。
对 DTD 的引用
此文件包含对 DTD 的引用:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"http://www.w3school.com.cn/dtd/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>对 XML Schema 的引用
此文件包含对 XML Schema 的引用:
<?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">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
========================================
4.XSD - <schema> 元素
<schema> 元素是每一个 XML Schema 的根元素。
<schema> 元素
<schema> 元素是每一个 XML Schema 的根元素:
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema><schema> 元素可包含属性。一个 schema 声明往往看上去类似这样:
<?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:schema>代码解释:
下面的片断:
xmlns:xs="http://www.w3.org/2001/XMLSchema"显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:
这个片断:
targetNamespace="http://www.w3school.com.cn" 显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。
这个片断:
xmlns="http://www.w3school.com.cn" 指出默认的命名空间是 "http://www.w3school.com.cn"。
这个片断:
elementFormDefault="qualified" 指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。
在 XML 文档中引用 Schema
此 XML 文档含有对 XML Schema 的引用:
<?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">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>代码解释:
下面的片断:
xmlns="http://www.w3school.com.cn" 规定了默认命名空间的声明。此声明会告知 schema 验证器,在此 XML 文档中使用的所有元素都被声明于 "http://www.w3school.com.cn" 这个命名空间。
一旦您拥有了可用的 XML Schema 实例命名空间:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 您就可以使用 schemaLocation 属性了。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:
xsi:schemaLocation="http://www.w3school.com.cn note.xsd"
- Schema入门之XMLSchema使用及元素
- Schema入门之XMLSchema元素及属性
- Schema入门之XMLSchema限定及复杂元素
- Schema入门之XMLSchema简介及使用原因
- 读取Schema文件(XmlSchema.Read())
- xml之schema入门
- Lorg/apache/ws/commons/schema/XmlSchema;
- 为何使用XMLSchema
- xmlschema
- xmlSchema
- xml-schema空元素约束及案例
- 使用 XML Schema 定义元素的基本知识
- 使用 XML Schema 定义元素的基本知识
- 使用 XML Schema 定义元素的基本知识
- 使用 XML Schema 定义元素的基本知识
- 使用 XML Schema 定义元素的基本知识
- Mondrian入门介绍之schema manager
- jaxb的@xmlSchema注解使用没有效果
- 分享别人的两种闹钟的实现方法
- Schema入门介绍
- Schema入门之XMLSchema简介及使用原因
- The programming language 1-13 竖直方向直方图
- 外交部:对莫言获得诺贝尔文学奖表示祝贺-外交部-莫言-祝贺
- Schema入门之XMLSchema使用及元素
- 苹果Mac OS X系统安全评级(1)
- Schema入门之XMLSchema元素及属性
- Schema入门之XMLSchema限定及复杂元素
- RTTI、虚函数和虚基类的实现方式、开销分析及使用指导
- 国家公务员考录比例可能将达90比1 -招考职位-国考-职位数
- 山东高密整理莫言旧居欲打造红高粱文化景点-莫言-诺贝尔文学奖-高密
- 十大英伦伤感歌曲
- 杭州西湖申遗成功后首次举办烟花大会-杭州-西湖-烟花大会