属性noNamespaceSchemaLocation对XML解析有什么影响?

12

定义:

noNamespaceSchemaLocation 属性引用一个没有目标命名空间的 XML Schema 文档。

这个属性如何会改变解析的结果?

例如,考虑以下 XML:

<?xml version="1.0"?>
<name
  xmlns="http://www.example.com/name"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.example.com/name schema/schema.xsd"
  title="Mr.">
   <first>John</first>
   <middle>M</middle>
   <last>Doe</last>
</name>

参考此架构:

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.example.com/name"
targetNamespace="http://www.example.com/name" elementFormDefault="qualified">
  <element name="name">
    <complexType>
      <sequence>
        <element name="first" type="string"/>
        <element name="middle" type="string"/>
        <element name="last" type="string"/>
      </sequence>
      <attribute name="title" type="string"/>
    </complexType>
  </element>
</schema>

我从模式中删除了这些命名空间声明:

xmlns:target="http://www.example.com/name" 
targetNamespace="http://www.example.com/name" 

在引用XML时,即使没有使用noNamespaceSchemaLocation属性,也不会抛出任何错误。我们究竟为什么需要这个属性呢?

1个回答

11
该属性对于XML解析器没有影响。如果设置了适当的选项,则它可能会影响XML Schema处理器的行为; 同样,它也可能影响将XML解析和XML模式验证功能组合在一起的程序的行为。它告诉模式处理器在哪里查找描述文档的模式。
但即使使用模式处理器,noNamespaceSchemaLocation属性也不会影响像您的文档这样所有元素都在命名空间中的文档的验证。

谢谢回复,迈克尔。我想我的问题应该是“属性noNamespaceSchemaLocation对XML验证有什么影响?”如果我的文档的一部分没有命名空间,你是否意味着这是noNamespaceSchemaLocation的作用发挥作用并解决数据和模式中重叠的“无命名空间”的时候? - Jops
4
属性告诉架构处理器在哪里查找可用于验证不属于任何命名空间的元素的模式。在您的示例中,没有这样的元素。 - Michael Kay
啊,太酷了。谢谢你的澄清,Michael。现在我完全明白了。 - Jops

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接