简体   繁体   English

无法针对xsd验证xml。 错误“ cvc-elt.1:找不到元素'systems'的声明”

[英]Cannot validate xml against a xsd. Error “cvc-elt.1: Cannot find the declaration of element 'systems'”

I am having trouble validating an XML using an XSD via code. 我无法通过代码使用XSD验证XML。 I can't figure out what I'm missing XML: 我不知道我缺少什么XML:

<?xml version="1.0" encoding="UTF-8"?>
<systems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="test.namespace">
  <system address="test" id="test" name="test" systemNr="test">
    <mandant mandant="test"/>
  </system>
  <system address="test2" name="test2" systemNr="test2" id="test2">
    <mandant mandant="test2"/>
    <mandant mandant="test2"/>
  </system>
  <system id="test3" address="test3" name="test3" systemNr="test3">
    <mandant mandant="test"/>
  </system>
</systems>

XSD: XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test.namespace">
  <xs:element name="systems">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="system" maxOccurs="unbounded" minOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="mandant"
                maxOccurs="unbounded" minOccurs="1">
                <xs:complexType>
                  <xs:attribute name="mandant"
                    type="xs:string" use="required">
                  </xs:attribute>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required">
            </xs:attribute>
            <xs:attribute name="name" type="xs:string" use="required">
            </xs:attribute>
            <xs:attribute name="address" type="xs:string" use="required">
            </xs:attribute>
            <xs:attribute name="systemNr"
              type="xs:string" use="required">
            </xs:attribute>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

And here is the code snippet: 这是代码片段:

      File systemsFile = new File(LocalFileSystemManager.getDefaultPath() + "Systems.xml");
      File schemaFile = new File(LocalFileSystemManager.getDefaultPath() + "SystemsSchema.xsd");
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document systemsDocument = db.parse(systemsFile);
      systemsDocument.getDocumentElement().normalize();
SchemaFactory factory = SchemaFactory
      .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      Schema schema = factory.newSchema(schemaFile);
      Validator validator = schema.newValidator();
      validator.validate(new DOMSource(systemsDocument));

Thanks in advance! 提前致谢! Lori 洛里

This instance should validate. 此实例应验证。 Your attributes belong to the schema so you need to mark them as such with a namespace: 您的属性属于架构,因此您需要使用名称空间对其进行标记:

<?xml version="1.0" encoding="UTF-8"?>
<systems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="test.namespace">
   <system a:address="test" a:id="test" a:name="test" a:systemNr="test" xmlns:a="test.namespace">
      <mandant a:mandant="test"/>
   </system>
   ...
</systems>

I am assuming you have attributeFormDefault="qualified" and elementFormDefault="qualified" in your <schema /> element? 我假设您在<schema />元素中有attributeFormDefault="qualified"elementFormDefault="qualified"吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cvc-elt.1:找不到元素&#39;staff&#39;的声明-无法针对xsd验证xml - cvc-elt.1: Cannot find the declaration of element 'staff' - Cannot validate xml against a xsd XML,XSD,cvc-elt.1:找不到元素的声明 - XML, XSD, cvc-elt.1: Cannot find the declaration of element 针对XSD的XML验证:cvc-elt.1:找不到元素&#39;xxx&#39;的声明 - XML validation against XSD: cvc-elt.1: Cannot find the declaration of element 'xxx' 使用cvc-elt.1进行xsd验证失败:无法找到元素的声明 - xsd validation fails with cvc-elt.1: Cannot find the declaration of element cvc-elt.1:找不到元素的声明 - cvc-elt.1: Cannot find the declaration of element 错误:cvc-elt.1:找不到元素“beans”的声明 - Error: cvc-elt.1: Cannot find the declaration of element 'beans' XSD验证错误:“ cvc-elt.1:找不到元素&#39;xs:schema&#39;的声明” - Error with XSD validation: “cvc-elt.1: Cannot find the declaration of element 'xs:schema'” 如何修复错误:cvc-elt.1:找不到没有命名空间的元素“xsd:schema”的声明? - How to fix the error: cvc-elt.1: Cannot find the declaration of element 'xsd:schema' without namespaces? cvc-elt.1:找不到RootElement的元素声明 - cvc-elt.1: Cannot find the declaration of element for RootElement cvc-elt.1:找不到元素&#39;NewIssue&#39;的声明 - cvc-elt.1: Cannot find the declaration of element 'NewIssue'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM