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

Jop*_*ops 8 xml

根据定义:

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>
Run Code Online (Sandbox Code Playgroud)

参考这个架构:

<?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>
Run Code Online (Sandbox Code Playgroud)

我从架构中删除了这些命名空间声明:

xmlns:target="http://www.example.com/name" 
targetNamespace="http://www.example.com/name" 
Run Code Online (Sandbox Code Playgroud)

甚至没有在引用XML中使用noNamespaceSchemaLocation属性,也没有抛出任何错误.为什么我们甚至首先需要这个属性?

Mic*_*Kay 8

该属性对XML解析器没有影响.如果设置了适当的选项,它可能会影响XML Schema Processor的行为; 它可能同样会影响程序的行为,该程序结合了XML解析和XML模式验证的功能.它告诉模式处理器在哪里查找描述文档的模式.

但即使使用模式处理器,该noNamespaceSchemaLocation属性也不会影响像您这样的文档的验证,其中元素都在命名空间中.

  • 该属性告诉架构处理器在哪里查找可用于验证没有命名空间的元素的架构.在您的示例中,没有任何此类元素. (3认同)