xml验证失败,需要属性且属性不允许错误,这是不明确的

时间:2015-06-24 06:46:30

标签: java xml validation xsd

我为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>

根据架构,id<department>代码需要属性<student>,我的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...]

不确定是什么问题。这两个错误消息都是矛盾的

如果我删除&#34; id&#34;验证失败属性来自和当我拥有它时

通过修改XSD,我可以通过修改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来验证XSD的XML 2.我在Altova XML Spy编辑器中也观察到同样的错误。

1 个答案:

答案 0 :(得分:1)

问题是属性id已被声明为全局(xs:schema的直接子)。因此,属性id应使用命名空间http://www.learnjava.com.进行限定。请注意idhttp://www.learnjava.com:id不同 如果您不希望属性id属于命名空间:

  1. 您应该使用本地属性声明
  2. 在架构文档中,更改

     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://www.learnjava.com"
                    xmlns="http://www.learnjava.com"
                    elementFormDefault="qualified"
                    attributeFormDefault="qualified">
    
  3.      <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>