我無法通過代碼驗證使用XSD的XML。我想不出什麼我失蹤 XML:無法針對xsd驗證xml。錯誤「cvc-elt.1:找不到元素'系統'的聲明」
<?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>
這裏是代碼片段:
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));
提前感謝! Lori
可能與命名空間有關嗎? –
抱歉,看起來我沒有從XSD Document中複製架構定義標記。它是:<?xml version =「1.0」encoding =「UTF-8」?> –
Lori