xml validation failed with attribute required and attribute not allowed 错误,这是不明确的
xml validation failed with attribute required and attribute not allowed error, which is ambiguous
我已经为 XML
定义了以下架构
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
attributeFormDefault="qualified">
<!-- simple elements -->
<xs:element name="name" type="xs:string"/>
<xs:element name="hod" type="xs:string"/>
<xs:element name="dept" type="xs:integer"/>
<!-- attributes -->
<xs:attribute name="id" type="xs:integer"/>
<!-- complex elements -->
<xs:element name="department">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="hod" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="departments">
<xs:complexType>
<xs:sequence>
<xs:element ref="department" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="dept"/>
</xs:sequence>
<xs:attribute ref="id" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element ref="student" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="school">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="departments"/>
<xs:element ref="students"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
根据架构,<department>
和 <student>
标签需要 id
属性,我下面的 XML 遵守此规则
<?xml version="1.0" encoding="UTF-8"?>
<school xmlns="http://www.learnjava.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
<name>TAMUC</name>
<departments>
<department id="1001">
<name>Computer Science</name>
</department>
<department id="1002">
<name>Social Science</name>
<hod>Jeff</hod>
</department>
</departments>
<students>
<student id="5001">
<name>Frank</name>
<dept>1001</dept>
</student>
<student id="5002">
<name>Paul</name>
<dept>1001</dept>
</student>
</students>
</school>
但是验证失败并出现以下错误
Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'department'.
Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.4: Attribute 'id' must appear on element 'department'.
[..further errors omited...]
不确定哪里出了问题。两种错误信息都是矛盾的
如果我删除 "id" 属性以及我拥有它时验证失败
我有一个变通办法来完成这项工作,方法是像下面那样修改 XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified">
<xs:element name="school">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="departments">
<xs:complexType>
<xs:sequence>
<xs:element name="department" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="hod" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="dept" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
但是作为一个XML新手,我很好奇之前的XSD
哪里出了问题
PS:
1. 我正在使用 http://www.utilities-online.info/xsdvalidation/#.VYpP8vmqqko 来验证 XML 针对 XSD.
2. 我在 Altova XML Spy 编辑器中也观察到了同样的错误。
问题是属性 id
已声明为全局属性(xs:schema 的直接子项)。因此,属性 id
应该使用命名空间 http://www.learnjava.com.
进行限定 注意 id
与 http://www.learnjava.com:id
不同
如果您不希望属性 id
属于命名空间:
- 你应该使用本地属性声明
在您的架构文档中,更改
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
attributeFormDefault="qualified">
到
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
>
更新:
如果您想使用全局属性声明,那么您的 xml 实例文档将如下所示:
<p:school xmlns:p="http://www.learnjava.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
<p:name>TAMUC</p:name>
<p:departments>
<p:department p:id="1001">
<p:name>Computer Science</p:name>
</p:department>
<p:department p:id="1002">
<p:name>Social Science</p:name>
<p:hod>Jeff</p:hod>
</p:department>
</p:departments>
<p:students>
<p:student p:id="5001">
<p:name>Frank</p:name>
<p:dept>1001</p:dept>
</p:student>
<p:student p:id="5002">
<p:name>Paul</p:name>
<p:dept>1001</p:dept>
</p:student>
</p:students>
</p:school>
我已经为 XML
定义了以下架构<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
attributeFormDefault="qualified">
<!-- simple elements -->
<xs:element name="name" type="xs:string"/>
<xs:element name="hod" type="xs:string"/>
<xs:element name="dept" type="xs:integer"/>
<!-- attributes -->
<xs:attribute name="id" type="xs:integer"/>
<!-- complex elements -->
<xs:element name="department">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="hod" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="departments">
<xs:complexType>
<xs:sequence>
<xs:element ref="department" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="dept"/>
</xs:sequence>
<xs:attribute ref="id" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element ref="student" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="school">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="departments"/>
<xs:element ref="students"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
根据架构,<department>
和 <student>
标签需要 id
属性,我下面的 XML 遵守此规则
<?xml version="1.0" encoding="UTF-8"?>
<school xmlns="http://www.learnjava.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
<name>TAMUC</name>
<departments>
<department id="1001">
<name>Computer Science</name>
</department>
<department id="1002">
<name>Social Science</name>
<hod>Jeff</hod>
</department>
</departments>
<students>
<student id="5001">
<name>Frank</name>
<dept>1001</dept>
</student>
<student id="5002">
<name>Paul</name>
<dept>1001</dept>
</student>
</students>
</school>
但是验证失败并出现以下错误
Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'department'.
Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.4: Attribute 'id' must appear on element 'department'.
[..further errors omited...]
不确定哪里出了问题。两种错误信息都是矛盾的
如果我删除 "id" 属性以及我拥有它时验证失败
我有一个变通办法来完成这项工作,方法是像下面那样修改 XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified">
<xs:element name="school">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="departments">
<xs:complexType>
<xs:sequence>
<xs:element name="department" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="hod" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="dept" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
但是作为一个XML新手,我很好奇之前的XSD
哪里出了问题PS:
1. 我正在使用 http://www.utilities-online.info/xsdvalidation/#.VYpP8vmqqko 来验证 XML 针对 XSD.
2. 我在 Altova XML Spy 编辑器中也观察到了同样的错误。
问题是属性 id
已声明为全局属性(xs:schema 的直接子项)。因此,属性 id
应该使用命名空间 http://www.learnjava.com.
进行限定 注意 id
与 http://www.learnjava.com:id
不同
如果您不希望属性 id
属于命名空间:
- 你应该使用本地属性声明
在您的架构文档中,更改
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.learnjava.com" xmlns="http://www.learnjava.com" elementFormDefault="qualified" attributeFormDefault="qualified">
到
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
>
更新:
如果您想使用全局属性声明,那么您的 xml 实例文档将如下所示:
<p:school xmlns:p="http://www.learnjava.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
<p:name>TAMUC</p:name>
<p:departments>
<p:department p:id="1001">
<p:name>Computer Science</p:name>
</p:department>
<p:department p:id="1002">
<p:name>Social Science</p:name>
<p:hod>Jeff</p:hod>
</p:department>
</p:departments>
<p:students>
<p:student p:id="5001">
<p:name>Frank</p:name>
<p:dept>1001</p:dept>
</p:student>
<p:student p:id="5002">
<p:name>Paul</p:name>
<p:dept>1001</p:dept>
</p:student>
</p:students>
</p:school>