XML验证 - 未声明的元素

时间:2018-05-15 13:01:58

标签: java xsd xml-validation

我没有得到这个xml:

<mappings>
 <mapping>
  <protobufMessage>pb::Header</protobufMessage>
  <rosPackage>std_msgs</rosPackage>
  <rosMessage>Header</rosMessage>
 </mapping>
</mappings>

针对此架构进行了验证,该架构在源代码中内联定义:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
>
<xs:element name="mappings">
    <xs:complexType>
        <xs:sequence>
                <xs:element name="mapping" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="protobufMessage" type="xs:string"/>
                            <xs:element name="rosPackage" type="xs:string"/>
                            <xs:element name="rosMessage" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

我收到错误消息

  

org.xml.sax.SAXParseException; cvc-elt.1:找不到声明   元素映射&#39;。在[...]

验证代码(xtend代码):

def private validate(InputStream is) {
    val parserF = DocumentBuilderFactory.newInstance
    val parser = parserF.newDocumentBuilder 
    val document = parser.parse(is)
    val validatorF = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    val schemaSource = new StreamSource(new StringReader(schema)) // schema is a String member which contains the schema
    val valSchema = validatorF.newSchema(schemaSource)
    val validator = valSchema.newValidator
    validator.validate(new DOMSource(document))
}

我在这里做错了什么?

0 个答案:

没有答案