我是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
答案 0 :(得分:0)
您能否分享您正在解析的XML文件? 此外,Trade元素所采用的“RecordForm”类型未在架构中定义。 尝试包括那个,看看。 应导入您正在使用的命名空间以区分XML中的元素。
答案 1 :(得分:0)
这是我身边的正确答案,肯定会帮助你:
首先,你所拥有的xml没有正确的语法,我在这里已经纠正过:
<TRANSACTIONS xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./transactions.xsd" ASOF_DATE="6/10/2011" CREATE_DATE="6/10/2011" RECORDS="1769">
<TRADE>
<ACCRUAL_DT>
09/22/2012
</ACCRUAL_DT>
<COUNTERPARTY_CODE>
US
</COUNTERPARTY_CODE>
<CUSIP>
BRS87R7N9
</CUSIP>
<DESC_INSTMT>
CFD COOKSON GROUP PLC
</DESC_INSTMT>
<DESK/>
</TRADE>
</TRANSACTIONS>
现在简单到网站:http://www.freeformatter.com/xsd-generator.html
输入上面提到的XML,然后单击Generate XSD Schema。所以现在会生成模式,然后将其保存在本地磁盘中,然后运行下面的代码来检查并验证它!
package your_package name;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
public class XMLValidator
{
public void validate() {
File xmlFile = new File(Provide the xml file location (disk) in double quotes);
File xsdFile = new File(Provide the xml file location (disk) in double quotes);
boolean retStat = this.validateSchema(xmlFile, xsdFile);
if(retStat){
System.out.println("Validated");
}
else
System.out.println("Not Valid");
}
private boolean validateSchema(File xml, File xsd){
Schema schema = null;
try{
schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd);
}catch (SAXException e) {
e.printStackTrace();
return false;
}catch(Exception fnEx){
fnEx.printStackTrace();
return false;
}
if(null != schema){
Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(xml));
return true;
} catch (SAXException e) {
return false;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public static void main(String args[]){
XMLValidator newObj=new XMLValidator();
newObj.validate();
}
}
并通过解决!!
答案 2 :(得分:0)
应导入您正在使用的命名空间以区分XML中的元素。
以下是重用ComplexTypes的XSD:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="TRANSACTIONS" type="RootForm" />
<xsd:complexType name="RootForm">
<xsd:sequence>
<xsd:element name="TRADE" type="RecordForm" />
</xsd:sequence>
<xsd:attribute name="xsi:noNamespaceSchemaLocation" type="xsd:string" />
<xsd:attribute name="ASOF_DATE" type="xsd:dateTime" />
<xsd:attribute name="CREATE_DATE" type="xsd:dateTime" />
<xsd:attribute name="RECORDS" type="xsd:int" />
</xsd:complexType>
<xsd:complexType name="RecordForm">
<xsd:sequence>
<xsd:element name="ACCRUAL_DT" type="xsd:dateTime" />
<xsd:element name="COUNTERPARTY_CODE" type="xsd:string" />
<xsd:element name="CUSIP" type="xsd:string" />
<xsd:element name="DESC_INSTMT" type="xsd:string" />
<xsd:element name="DESK" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>