【问题标题】:cvc-elt.1: Cannot find the declaration of element 'staff' - Cannot validate xml against a xsdcvc-elt.1:找不到元素“员工”的声明 - 无法针对 xsd 验证 xml
【发布时间】:2014-01-27 15:34:10
【问题描述】:

我无法根据 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)

这里是sn-p的代码:

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】:

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

    <?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>
    

    【讨论】:

    • 我试过这个,但结果是一样的。你能推荐任何从xml文件生成xsd的工具吗?我试过 one - trang,但它看起来对人类来说太难理解了。
    • 对不起,我的回答是完全错误的。我认为您的架构没问题,问题出在您的 Java 代码中。抱歉,我没有时间为您调试。
    猜你喜欢
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2020-05-08
    • 2015-11-25
    • 2015-02-09
    相关资源
    最近更新 更多