我在针对XSD验证XML时遇到问题。验证器抛出
前缀" xsi" for attribute" xsi:schemaLocation"与...相关联 元素类型" 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>
答案 0 :(得分:7)
&#34;前缀&#34; xsi&#34; for attribute&#34; xsi:schemaLocation&#34;与元素类型相关联&#34; mpreader&#34;不受约束。&#34;
亲爱的达兰,亲爱的达兰,亲爱的达兰,然后把它捆绑起来......只需添加一个将前缀xsi绑定到命名空间http://www.w3.org/2001/XMLSchema-instance
的命名空间声明(https://en.wikipedia.org/wiki/There%27s_a_Hole_in_My_Bucket)
答案 1 :(得分:4)
您的XML应声明xsi的命名空间 例如的xmlns:的xsi = “http://www.w3.org/2001/XMLSchema-instance”
答案 2 :(得分:4)
快速回答:修正您使用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">
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>
将验证以下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>