cvc-elt.1:无法找到元素'staff'的声明 - 无法对xsd验证xml

时间:2014-01-27 15:34:11

标签: java xml validation xsd

我无法针对xsd架构验证xml文档。

我无法弄清楚那里有什么问题。

这是xml档案:

<?xml version="1.0" encoding="UTF-8"?>
<staff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="employee.xsd">
    <employee>
        <name>Carl Cracker</name>
        <salary>75000</salary>
        <hiredate year="1987" month="12" day="15" />
    </employee>
    <employee>
        <name>Harry Hacker</name>
        <salary>50000</salary>
        <hiredate year="1989" month="10" day="1" />
    </employee>
    <employee>
        <name>Tony Tester</name>
        <salary>40000</salary>
        <hiredate year="1990" month="3" day="15" />
    </employee>
</staff>

xsd档案:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="staff">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="employee" maxOccurs="unbounded" minOccurs="0">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element type="xsd:string" name="name"/>
              <xsd:element type="xsd:int" name="salary"/>
              <xsd:element name="hiredate">
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                      <xsd:attribute type="xsd:short" name="year" use="optional"/>
                      <xsd:attribute type="xsd:byte" name="month" use="optional"/>
                      <xsd:attribute type="xsd:byte" name="day" use="optional"/>
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

接下来是输出:

cvc-elt.1: Cannot find the declaration of element 'staff'.
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
    at com.lab.edu.DOMTreeParser.parseStaff(DOMTreeParser.java:77)
    at com.lab.edu.DOMTreeParser.<init>(DOMTreeParser.java:42)
    at com.lab.edu.Application.main(Application.java:7)

以下是代码片段:

public DOMTreeParser(String filename) {
    employeeList = new ArrayList<>();
    boolean result = validate(filename);
    System.out.println("it is validate result: " + result);

    if (result == false) {
        System.out.println("XML isn't valid");
        System.exit(0);
    }
    // if all are correct - parse xml
    parseStaff(document.getDocumentElement());
}

private boolean validate(String filename) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
        factory.setIgnoringElementContentWhitespace(true);

        builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new SimpleErrorHandler());

        document = builder.parse(new File(filename));

        return true;
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
        return false;
    }
}

我尝试在validator验证xml协议xsd - 一切都有效。

cvs..消息的原因是什么?

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

实例文档的最外层元素必须与架构中的全局元素声明匹配。您只有一个全局元素声明,对于&#34; staff&#34;元素,在您的实例中不存在。如果你想要&#34;员工&#34;要验证的元素,您必须将它从本地元素声明提升为全局元素声明,这意味着将模式重写为:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="staff">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="employee" maxOccurs="unbounded" minOccurs="0"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="employee">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element type="xsd:string" name="name"/>
              <xsd:element type="xsd:int" name="salary"/>
              <xsd:element name="hiredate">
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                      <xsd:attribute type="xsd:short" name="year" use="optional"/>
                      <xsd:attribute type="xsd:byte" name="month" use="optional"/>
                      <xsd:attribute type="xsd:byte" name="day" use="optional"/>
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
   </xsd:element>
</xsd:schema>