我是 XML 验证的新手。
我的 XSD 是
<xsd:complexType name="RootForm">
<xsd:sequence>
<xsd:element name="TRADE" type="RecordForm" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="ASOF_DATE" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="CREATE_DATE" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="RECORDS" type="xsd:integer" use="required"/>
</xsd:complexType>
我正在运行的代码是:
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
InputSource i = new InputSource("X:/workspace/XMLValidation/src/xml/trades.xml");
InputSource i1 = new InputSource("X:/workspace/XMLValidation/src/xml/transactions.xsd");
SAXParser saxParser = spf.newSAXParser();
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", i1);
XMLReader xmlReader = saxParser.getXMLReader();
// xmlReader.setContentHandler(new SimpleErrorHandler());
xmlReader.setErrorHandler(new SimpleErrorHandler());
try {
xmlReader.parse(i);
} catch (IOException e) {
e.printStackTrace();
}
我得到以下异常:
src-resolve.4.2: Error resolving component 'RootForm'. It was detected that 'RootForm' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'. If this is the incorrect namespace, perhaps the prefix of 'RootForm' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'.
Public ID: null
System ID: file:///X:/workspace/XMLValidation/src/xml/transactions.xsd
Line number: 6
Column number: 52
Message: src-resolve.4.2: Error resolving component 'RootForm'. It was detected that 'RootForm' is in namespace 'http://www.w3schools.com', but components from this namespace are not referenceable from schema document 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'. If this is the incorrect namespace, perhaps the prefix of 'RootForm' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///X:/workspace/XMLValidation/src/xml/transactions.xsd'.
cvc-elt.1: Cannot find the declaration of element 'TRANSACTIONS'.
谁能帮我在xsd文件中做错了什么
谢谢 Avnish