繁体   English   中英

没有schemaLocation的XMLSchema和XMLSchema实例名称空间

[英]XMLSchema and XMLSchema-instance namespaces without schemaLocation

我对xml命名空间的问题很少,我将用这三段代码解释:

1 - 非常简单的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架构的XML:

<?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 - 与上述两个没有关系的另一个片段代码:

<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>

问题是:

  1. 为什么我们总是声明名称空间,如xmlns =“http://www.w3.org/2001/XMLSchema”xmlns:xsi =“http://www.wc3.org/2001/XMLSchema-instance”但没有schemaLocation为给出了这些?
  2. XML解析器如何知道(例如验证) xmlns =“http://www.w3.org/2001/XMLSchema”定义<attribute><complexType><sequence>等元素?
  3. 阅读很多帖子我理解名称空间,因此它们的URI,基本上没什么意思,它们只是用来避免名称冲突。 但我还读到,如果你声明xmlns =“http://www.w3.org/2001/XMLSchema”命名空间错误,XML文件将无效,为什么?
  4. 为什么在最后一个代码片段中始终没有为http://www.w3.org/2001/XMLSchema-instance提供 schemaLocation
  1. 这些内置命名空间与XSD组件本身有关。 不需要schemaLocation ,因为XML Schema Recommendation暗示了它们的定义。
  2. 根据定义,符合条件的XML解析器将理解xs:attribute等的含义。
  3. 我不会说名称空间毫无意义。 除了作为区分其他识别命名组件的方法之外,名称空间还可用于将组件的使用与其在另一个XSD中的集合定义相关联。
  4. 如#1中所述, http://www.w3.org/2001/XMLSchema-instance是一个内置命名空间,由XML Schema Recommendation暗示的组件组成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM