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

放肆的年华 提交于 2019-12-24 10:16:45

问题


I am having trouble validating an XML using an XSD via code. I can't figure out what I'm missing 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:

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


回答1:


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?



来源:https://stackoverflow.com/questions/7568574/cannot-validate-xml-against-a-xsd-error-cvc-elt-1-cannot-find-the-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!