错误:未绑定属性"xsi:schemaLocation"的前缀"xsi"

Dal*_*ker 5 xml xsd xml-validation

我在针对XSD验证XML时遇到问题.验证器抛出

与元素类型"mpreader"相关联的属性"xsi:schemaLocation"的前缀"xsi"未绑定.

这是一个XML剪辑

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mpreader xmlns="C:\Users\Dallan\Desktop\Mpreader\" xmlns:xs="http://www.w3.org/20one/XMLSchema-instance" 
 xsi:schemaLocation="C:\Users\Dallan\Desktop\Mpreader\mpreaderschemafinal.xsd"> 
                <firmware>"3.4.16"</firmware>  

                <hardware>"2.3.53"</hardware>  

                <sn>"234-1three5"</sn>

                <devices> 
            </devices>
        </mpreader>
Run Code Online (Sandbox Code Playgroud)

这是XSD剪辑

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="C:Users/Dallan/Desktop/Mpreader/" elementFormDefault="qualified" targetNamespace="C:\Users\Dallan\Desktop\Mpreader\">

<xs:element name="mpreader">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
    <xs:element name="firmware" type="xs:string"/>
    <xs:element name="hardware" type="xs:string"/>
    <xs:element name="sn" type="xs:string"/>
    <xs:element name="devices">
        <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

Mic*_*Kay 8

"与元素类型"mpreader"相关联的属性"xsi:schemaLocation"的前缀"xsi"未绑定."

然后绑定它,亲爱的达兰,亲爱的达兰,亲爱的达兰...

只需添加一个命名空间声明,将前缀xsi绑定到命名空间http://www.w3.org/2001/XMLSchema-instance

(https://en.wikipedia.org/wiki/There%27s_a_Hole_in_My_Bucket)


srj*_*rjt 5

您的 XML 应该为 xsi 声明命名空间,例如 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


kjh*_*hes 5

快速回答: 修复您的使用方式xsi:schemaLocation

<mpreader xmlns="C:\Users\Dallan\Desktop\Mpreader\"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="C:\Users\Dallan\Desktop\Mpreader\
                              C:\Users\Dallan\Desktop\Mpreader\mpreaderschemafinal.xsd">
Run Code Online (Sandbox Code Playgroud)

细节

  • 声明一个xsi xs)命名空间前缀以匹配其在 xsi:schemaLocation.
  • xsi为: 声明正确的命名空间http://www.w3.org/2001/XMLSchema-instance,而不是 http://www.w3.org/20one/XMLSchema-instance.
  • 将 的值更改xsi:schemaLocation为名称空间-位置
  • 删除其中的空白devices(尽管这可能只是修剪的产物)。

您的 XSD 中还缺少一个结束xs:sequence标签(但是,这可能只是一个修剪错误):

总而言之,以下 XSD,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           targetNamespace="C:\Users\Dallan\Desktop\Mpreader\">

  <xs:element name="mpreader">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="firmware" type="xs:string"/>
        <xs:element name="hardware" type="xs:string"/>
        <xs:element name="sn" type="xs:string"/>
        <xs:element name="devices">
          <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

将验证以下 XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mpreader xmlns="C:\Users\Dallan\Desktop\Mpreader\"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="C:\Users\Dallan\Desktop\Mpreader\
                              C:\Users\Dallan\Desktop\Mpreader\mpreaderschemafinal.xsd">
  <firmware>"3.4.16"</firmware>  
  <hardware>"2.3.53"</hardware>  
  <sn>"234-1three5"</sn>
  <devices/> 
</mpreader>
Run Code Online (Sandbox Code Playgroud)