1

我是 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

4

3 回答 3

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();
}

}

并通过解决!

于 2012-09-25T10:06:34.453 回答
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>
于 2012-09-26T13:11:44.223 回答
0

您能否分享您正在解析的 XML 文件?Trade 元素采用的“RecordForm”类型也未在模式中定义。尝试包括它并查看。应导入您正在使用的名称空间以区分 XML 中的元素。

于 2012-09-25T07:44:16.347 回答