xml模式文档(xml:Schema)详解
2015-01-30 14:09
295 查看
XML Schema 是基于 XML 的 DTD 替代者。
XML Schema 描述 XML 文档的结构。
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。
下面的例子是一个XML Schema文件,名为"note.xsd"
[html] view
plaincopy
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xsd:string"/>
<xsd:element name="from" type="xsd:string"/>
<xsd:element name="heading" type="xsd:string"/>
<xsd:element name="body" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
下面的XML文档和上文给出的XML
Schema相关联,名为"note.xml"。并且下文的讨论将围绕这两个文档展开。
[html] view
plaincopy
<?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>
note.xsd中,此片段:xmlns:xsd="http://www.w3.org/2001/XMLSchema",表明此schema中使用的元素和数据类型来自于"http://www.w3.org/2001/XMLSchema"名称空间(namespace)。它同样指出来自于"http://www.w3.org/2001/XMLSchema"名称空间的元素和数据类型必须使用带"xsd:
"前缀。作为名称空间的标识符(在声明中作为元素或属性的前缀),你也可以不使用xsd或xsi。这个 xmlns属性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。
对于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必须包含这个名称空间:http://www.w3.org/2001/XMLSchema。即此名称空间是由XML模式规范定义的标准名称空间-所有XML模式元素必须属于该名称空间。
此片段:targetNamespace="http://www.w3schools.com",表明此schema
(note, to, from, heading, body)定义的元素来自于"http://www.w3schools.com"名称空间。这个targetNamespace属性表示了该schema所对应的名称空间的URI。也就是说在引用该Schema的其它文档(包括自身文档)中要声明名称空间,其URI应该是targetNamespace的属性值。例如在这里因为要用到note.xsd自己定义的扩展数据类型(note,
to, from, heading, body),所以也声明了名称空间xmlns="http://www.w3schools.com"。而且该名称空间是默认名称空间(没有前缀)。targetNamespace属性为在模式中显式创建的所有新类型均声明了XML名称空间。
xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,类型等都属于该targetNameSpace,其自身或外部xsd文件使用这些元素,属性等都必须从定义的targetNameSpace中找。修改一下note.xsd,去除默认名称空间的声明,并添加一个复杂类型:
[html] view
plaincopy
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xs:string"/>
<xsd:element name="from" type="xs:string"/>
<xsd:element name="heading" type="xs:string"/>
<xsd:element name="body" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Student" type="stu"/>
<xsd:complexType name="stu">
<xsd:sequence>
<xsd:element name="Name" type="xs:string"/>
<xsd:element name="Class" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
上述代码中,复杂类型stu是找不到的,因为你定义了一个名称空间"http://www.w3schools.com",该复杂类型存在于"http://www.w3schools.com"中,因此应该修改代码如下:
[html] view
plaincopy
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns:student="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xs:string"/>
<xsd:element name="from" type="xs:string"/>
<xsd:element name="heading" type="xs:string"/>
<xsd:element name="body" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Student" type="student:stu"/>
<xsd:complexType name="stu">
<xsd:sequence>
<xsd:element name="Name" type="xs:string"/>
<xsd:element name="Class" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。
通过上面的例子,我们可以很深刻的理解targetNameSpace。targetNamespace定义了Schema定义的新元素与属性的名称空间。而"http://www.w3.org/2001/XMLSchema"名称空间则定义了element,
attribute, complexType, group, simpleType等元素。
在实例中引用模式文档
XML Schema提供了两个在实例文档中使用的特殊属性,用于指出模式文档的位置。这两个属性是:xsi:schemaLocation和xsi:noNamespaceSchemaLocation,前者用于声明了目标名称空间的模式文档,后者用于没有目标名称空间的模式文档,它们通常在实例文档中使用。
4.5.7.1 xsi:schemaLocation属性
xsi:schemaLocation属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是名称空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配。我们看例4-28。
例4-28 book6.xml
① 声明默认的名称空间(http://www.sunxin.org/book)。
② 声明XML Schema实例名称空间(http://www.w3.org/2001/XMLSchema-instance),并将xsi前缀与该名称空间绑定,这样模式处理器就可以识别xsi:schemaLocation属性。XML
Schema实例名称空间的前缀通常使用xsi。
③ 使用xsi:schemaLocation属性指定名称空间http://www.sunxin.org/book和模式位置http://www.sunxin.org/book.xsd相关。要注意,在这个例子中,book.xsd中声明的目标名称空间要求是http://www.sunxin.org/book。
一个可能的模式文档book.xsd如例4-29所示。
例4-29 book.xsd
实际上,xsi:schemaLocation属性的值也可以由多个URI引用对组成,每个URI引用对之间使用空白符分隔。例4-30的实例文档使用了多个名称空间,xsi:schemaLocation属性的值包含了两对URI。
例4-30 books.xml
XML Schema推荐标准中指出,xsi:schemaLocation属性可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:schemaLocation属性必须出现在它要验证的任何元素和属性之前。不过一般我们把它放在根元素上
此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:schemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:schemaLocation属性。
xsi:noNamespaceSchemaLocation属性
xsi:noNamespaceSchemaLocation属性用于引用没有目标名称空间的模式文档。与xsi:schemaLocation属性不同的是,xsi:noNamespaceSchemaLocation属性的值是单一的值,只是用于指定模式文档的位置。例4-31显示了在实例文档中xsi:noNamespaceSchema Location属性的使用。
例4-31 book7.xml
与xsi:schemaLocation属性一样,xsi:noNamespaceSchemaLocation属性也可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:noNamespaceSchemaLocation属性必须出现在它要验证的任何元素和属性之前。不过一般情况下放在根元素下
此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:noNamespaceSchemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:noNamespaceSchemaLocation属性
XML Schema 描述 XML 文档的结构。
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。
下面的例子是一个XML Schema文件,名为"note.xsd"
[html] view
plaincopy
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xsd:string"/>
<xsd:element name="from" type="xsd:string"/>
<xsd:element name="heading" type="xsd:string"/>
<xsd:element name="body" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
下面的XML文档和上文给出的XML
Schema相关联,名为"note.xml"。并且下文的讨论将围绕这两个文档展开。
[html] view
plaincopy
<?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>
note.xsd中,此片段:xmlns:xsd="http://www.w3.org/2001/XMLSchema",表明此schema中使用的元素和数据类型来自于"http://www.w3.org/2001/XMLSchema"名称空间(namespace)。它同样指出来自于"http://www.w3.org/2001/XMLSchema"名称空间的元素和数据类型必须使用带"xsd:
"前缀。作为名称空间的标识符(在声明中作为元素或属性的前缀),你也可以不使用xsd或xsi。这个 xmlns属性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。
对于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必须包含这个名称空间:http://www.w3.org/2001/XMLSchema。即此名称空间是由XML模式规范定义的标准名称空间-所有XML模式元素必须属于该名称空间。
此片段:targetNamespace="http://www.w3schools.com",表明此schema
(note, to, from, heading, body)定义的元素来自于"http://www.w3schools.com"名称空间。这个targetNamespace属性表示了该schema所对应的名称空间的URI。也就是说在引用该Schema的其它文档(包括自身文档)中要声明名称空间,其URI应该是targetNamespace的属性值。例如在这里因为要用到note.xsd自己定义的扩展数据类型(note,
to, from, heading, body),所以也声明了名称空间xmlns="http://www.w3schools.com"。而且该名称空间是默认名称空间(没有前缀)。targetNamespace属性为在模式中显式创建的所有新类型均声明了XML名称空间。
重点理解Schema文档使用自身定义类型
xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,类型等都属于该targetNameSpace,其自身或外部xsd文件使用这些元素,属性等都必须从定义的targetNameSpace中找。修改一下note.xsd,去除默认名称空间的声明,并添加一个复杂类型:[html] view
plaincopy
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xs:string"/>
<xsd:element name="from" type="xs:string"/>
<xsd:element name="heading" type="xs:string"/>
<xsd:element name="body" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Student" type="stu"/>
<xsd:complexType name="stu">
<xsd:sequence>
<xsd:element name="Name" type="xs:string"/>
<xsd:element name="Class" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
上述代码中,复杂类型stu是找不到的,因为你定义了一个名称空间"http://www.w3schools.com",该复杂类型存在于"http://www.w3schools.com"中,因此应该修改代码如下:
[html] view
plaincopy
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns:student="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xs:string"/>
<xsd:element name="from" type="xs:string"/>
<xsd:element name="heading" type="xs:string"/>
<xsd:element name="body" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Student" type="student:stu"/>
<xsd:complexType name="stu">
<xsd:sequence>
<xsd:element name="Name" type="xs:string"/>
<xsd:element name="Class" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。
通过上面的例子,我们可以很深刻的理解targetNameSpace。targetNamespace定义了Schema定义的新元素与属性的名称空间。而"http://www.w3.org/2001/XMLSchema"名称空间则定义了element,
attribute, complexType, group, simpleType等元素。
xsi:schemaLocation详解
在实例中引用模式文档XML Schema提供了两个在实例文档中使用的特殊属性,用于指出模式文档的位置。这两个属性是:xsi:schemaLocation和xsi:noNamespaceSchemaLocation,前者用于声明了目标名称空间的模式文档,后者用于没有目标名称空间的模式文档,它们通常在实例文档中使用。
4.5.7.1 xsi:schemaLocation属性
xsi:schemaLocation属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是名称空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配。我们看例4-28。
例4-28 book6.xml
<?xml version="1.0" encoding="GB2312"?> <book xmlns="http://www.sunxin.org/book" ① xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ② xsi:schemaLocation="http://www.sunxin.org/book http://www.sunxin.org/ book.xsd"> ③ <title>《Struts 2深入详解》</title> <author>孙鑫</author> </book> |
② 声明XML Schema实例名称空间(http://www.w3.org/2001/XMLSchema-instance),并将xsi前缀与该名称空间绑定,这样模式处理器就可以识别xsi:schemaLocation属性。XML
Schema实例名称空间的前缀通常使用xsi。
③ 使用xsi:schemaLocation属性指定名称空间http://www.sunxin.org/book和模式位置http://www.sunxin.org/book.xsd相关。要注意,在这个例子中,book.xsd中声明的目标名称空间要求是http://www.sunxin.org/book。
一个可能的模式文档book.xsd如例4-29所示。
例4-29 book.xsd
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sunxin.org/book" targetNamespace="http://www.sunxin.org/book" elementFormDefault="qualified"> <xs:element name="book" type="bookType"/> <xs:complexType name="bookType"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> |
例4-30 books.xml
<?xml version="1.0" encoding="GB2312"?> <books xmlns="http://www.sunxin.org/bks" xmlns:p="http://www.sunxin.org/people" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sunxin.org/bks bks.xsd http://www.sunxin.org/people people.xsd"> <book> <title>JSP深入编程</title> <author> <p:name>张三</p:name> <p:title>作家</p:title> </author> </book> <book> <title>XML从入门到精通</title> <author> <p:name>李四</p:name> <p:title>教师</p:title> </author> </book> </books> |
此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:schemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:schemaLocation属性。
xsi:noNamespaceSchemaLocation属性
xsi:noNamespaceSchemaLocation属性用于引用没有目标名称空间的模式文档。与xsi:schemaLocation属性不同的是,xsi:noNamespaceSchemaLocation属性的值是单一的值,只是用于指定模式文档的位置。例4-31显示了在实例文档中xsi:noNamespaceSchema Location属性的使用。
例4-31 book7.xml
<?xml version="1.0" encoding="GB2312"?> <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="book.xsd" isbn="978-7-121-06812-6" > <title>《Struts 2深入详解》</title> <author>孙鑫</author> </book> |
此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:noNamespaceSchemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:noNamespaceSchemaLocation属性
相关文章推荐
- 黑马程序员_Schema解析XML文档详解
- 使用schema验证xml文档的合法性
- XML详解----Schema
- C#中XML文档生成实例:Elevator Scheduler测试文件的生成详解
- xml通过schema方式的文档定义xsd,及使用示例
- AndroidManifest.xml文档详解
- 利用 JAXB 通过 XML 模式生成 XML 文档
- XML详解----Schema
- [转]XML详解--Schema
- [经验总结] 在Windows平台基于libxml+libxslt实现Schema模式验证与XSLT模板转换
- FusionMaps地图XML文档详解
- XMLEncoder生成的xml文档的schema分析
- 根据 XML 模式验证 XML 文档
- 采用Xerces C++执行基于schema的XML文档校验
- XML对Schema文档的引用123
- 利用JAXB通过XML模式生成XML文档
- XML详解----Schema
- XML详解----Schema
- xml--schema详解
- XML详解----Schema