我试图把一些验证代码放在一起。我试图验证对像模式:对复杂类型的javax.xml.parsers.DocumentBuilder支持
<?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);
没有人有关于如何才能做到这一点的任何想法? (或支持这种行为的验证?)
感谢
罗布
感谢您的建议,我曾尝试以前有元素的情况下,他们确实工作。然而,我有一个情况,我想验证这种类型,并希望有一种方式支持。 – Rob 2010-10-13 14:31:42
@RobL:不,这不是XML Schema的工作原理。您针对元素进行验证,而不是类型。 – skaffman 2010-10-13 14:34:01