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

问题描述 投票:0回答:1

我无法针对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..消息的原因是什么?

有什么建议?

java xml validation xsd
1个回答
0
投票

实例文档的最外层元素必须与架构中的全局元素声明匹配。对于“staff”元素,您只有一个全局元素声明,它在您的实例中不存在。如果您希望“employee”元素进行验证,则必须将其从本地元素声明提升为全局元素,这意味着将模式重写为:

<?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>
© www.soinside.com 2019 - 2024. All rights reserved.