Lori
Lori

Reputation: 562

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

Upvotes: 1

Views: 8620

Answers (1)

tom redfern
tom redfern

Reputation: 31770

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?

Upvotes: 1

Related Questions