【问题标题】:Error: The prefix "xsi" for attribute "xsi:schemaLocation" is not bound错误:属性“xsi:schemaLocation”的前缀“xsi”未绑定
【发布时间】:2015-08-03 23:38:35
【问题描述】:

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

属性“xsi:schemaLocation”的前缀“xsi”与 元素类型“mpreader”未绑定。

这是一个 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>

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

【问题讨论】:

    标签: xml xsd xml-validation


    【解决方案1】:

    “与元素类型“mpreader”关联的属性“xsi:schemaLocation”的前缀“xsi”未绑定。”

    那就绑定吧,亲爱的达兰,亲爱的达兰,亲爱的达兰……

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

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

    【讨论】:

    • 哈哈是的,我意识到我犯了一个愚蠢的错误,用 xs 代替了 xsi
    【解决方案2】:

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

    【讨论】:

    • 我的 xml 有 xmlns:xsi="w3.org/2001/XMLSchema-instance",但解析器仍然在抱怨 javax.xml.bind.UnmarshalException\n - 带有链接异常:\n[org.xml.sax.SAXParseException;行号:15;列号:256;与元素类型 \"PropertyType\" 关联的属性 \"xsi:nil\" 的前缀 \"xsi\" 未绑定。
    【解决方案3】:

    快速解答:修正你使用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">
    

    详情

    • 声明一个 xsinot 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>
    

    将验证以下 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-12
      • 2018-09-12
      • 1970-01-01
      • 2015-06-14
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多