我对xml命名空间的问题很少,我将用这三段代码解释:
1 - Very simple XML Schema:
<?xml version="1.0" encoding="US-ASCII"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.library.com"
targetNamespace="http://www.library.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<element name="Book" type="tns:BookType" />
<complexType name="BookType">
<sequence>
<element name="Title" type="string" />
<element name="Author" type="string" />
</sequence>
</complexType>
</schema>
2 - XML that use the newly created xml schema:
<?xml version="1.0" encoding="US-ASCII"?>
<Book xmlns:xsi="http://www.wc3.org/2001XMLSchema-instance"
xsi:schemaLocation="http://www.library.com ex9.xsd"
xmlns="http://www.library.com">
<Title>Scherlock Holmes</Title>
Author>Arthur Conan Doyle</Author>
</Book>
3 - Another fragment code without relationship from the two above:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
....
</beans>
问题是:
-
为什么我们总是声明名称空间如 xmlns="http://www.w3.org/2001/XMLSchema" 和 xmlns:xsi="http://www.wc3.org/2001/XMLSchema-instance" 但是没有给出 schemaLocation ?
-
XML解析器如何知道(例如,验证) xmlns="http://www.w3.org/2001/XMLSchema" 定义了
<attribute>
,<complexType>
,<sequence>
等元素? -
阅读很多帖子我理解名称空间及其URI,基本上没有任何意义,它们仅用于避免名称冲突 . 但我还读到,如果你声明 xmlns="http://www.w3.org/2001/XMLSchema" 命名空间错误,XML文件将无效,为什么?
-
为什么在最后一个代码片段中始终没有为 http://www.w3.org/2001/XMLSchema-instance. 给出schemaLocation
1 回答
这些内置命名空间与XSD组件本身有关 . 不需要
schemaLocation
,因为XML Schema Recommendation暗示了它们的定义 .根据定义,符合条件的XML解析器将理解
xs:attribute
等的含义 .我不会说名称空间没有任何意义 . 除了作为区分其他识别命名组件的方法之外,名称空间还可用于将组件的使用与其在另一个XSD中的集合定义相关联 .
如#1中所述,
http://www.w3.org/2001/XMLSchema-instance
是一个内置命名空间,由XML Schema Recommendation暗示的组件组成 .