我正在尝试整理一些验证代码。我正在尝试针对以下架构进行验证:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:choice="http://example.com/SimpleChoice" targetNamespace="http://example.com/SimpleChoice" ecore:nsPrefix="choice" ecore:package="com.example.simple.choice">
<xsd:complexType name="Plane">
<xsd:sequence>
<xsd:element name="freightDetails" type="xsd:string" minOccurs="0"/>
<xsd:element name="passengers" type="xsd:int" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
使用以下 XML:
<?xml version="1.0" encoding="UTF-8"?>
<choice:Plane xmlns:choice="http://example.com/SimpleChoice">
<freightDetails>Boxes</freightDetails>
</choice:Plane>
似乎抱怨没有元素,但我试图找到一种方法来验证类型。我收到以下错误:
[Error] :1:100: cvc-elt.1: Cannot find the declaration of element 'choice:Plane'.
当它尝试使用以下代码加载文档时:
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(schemaFile);
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
parserFactory.setSchema(schema);
parserFactory.setNamespaceAware(true);
DocumentBuilder parser = parserFactory.newDocumentBuilder();
Document document = parser.parse(inputSource);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
当它到达时失败:
Document document = parser.parse(inputSource);
有没有人对我如何能够让它工作有任何想法?(或者支持这种行为的验证器?)
谢谢
抢