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ファイルで間違っていることを教えてください。

ありがとう

4

3 に答える 3

0

解析している XML ファイルも共有していただけますか? また、Trade 要素が取る「RecordForm」タイプは、スキーマで定義されていません。それを含めてみてください。XML 内の要素を区別するために、使用している名前空間をインポートする必要があります。

于 2012-09-25T07:44:16.347 に答える
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 を入力し、[XSD スキーマの生成] をクリックします。これでスキーマが生成され、その後ローカル ディスクに保存され、以下のコードを実行してチェックと検証が行われます。

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 に答える