2016-12-07 5 views
0

JSONIXサンプルの購入注文を実行しようとしました。 ハイスコアウェブサイトで言及されたように私はそれをしました。 XSDの使用に関するこのサンプルベースが不思議でしたが、着信XMLの検証は、子ノードを持つ要素には使用されていましたが、単純なタグには使用されていませんでした。JSONIX強力なXML検証の方法

これは、エラーが表示されます:

... <item_will_cause_error partNum="926-AA"> 
     <productName>Baby Monitor</productName> 
     <quantity>1</quantity> 
     <USPrice>39.98</USPrice> 
     <shipDate>1999-05-21</shipDate> 
... </item_will_cause_error> 

これはない:<quantity_will_cause_error>が有効な要素ではありませんので、だから、

... <item partNum="926-AA"> 
     <productName>Baby Monitor</productName> 
     <quantity_will_cause_error>1</quantity_will_cause_error> 
     <USPrice>39.98</USPrice> 
     <shipDate>1999-05-21</shipDate> 
... </item> 

、それは、強力な検証に切り替えることが可能です。

種類がマルクス

答えて

0

に関しては、今、私はこの

var Jsonix = require('jsonix').Jsonix; 
//Include or require PO.js so that PO variable is available 
//For instance, in node.js: 
var PO = require('./mappings/PO').PO; 

    //First we construct a Jsonix context - a factory for unmarshaller  
    //(parser) 
    //and marshaller (serializer) 
    var context = new Jsonix.Context([ PO ]); 

    //Then we create a unmarshaller 
    var unmarshaller = context.createUnmarshaller(); 

    //Unmarshal an object from the XML retrieved from the URL 

    var fs = require('fs'); 
    var Ajv = require('ajv'); 

    var XMLSchemaJsonSchema =  
    JSON.parse(fs.readFileSync(
    './node_modules/jsonix/jsonschemas/w3c/2001/XMLSchema.jsonschema') 
    .toString()); 
    var JsonixJsonSchema = JSON.parse(fs.readFileSync(
    './node_modules/jsonix/jsonschemas/jsonix/Jsonix.jsonschema') 
    .toString()); 
    var POJsonSchema =   JSON.parse(fs.readFileSync(
    './mappings/PO.jsonschema').toString()); 

    var ajv = new Ajv(); 
    ajv.addSchema(XMLSchemaJsonSchema, 
    'http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema'); 
    ajv.addSchema(JsonixJsonSchema, 
    'http://www.jsonix.org/jsonschemas/jsonix/Jsonix.jsonschema'); 
    var validate = ajv.compile(POJsonSchema); 



    unmarshaller.unmarshalFile('./po.xml', 
    //This callback function will be provided 
    //with the result of the unmarshalling 
    function (unmarshalled) { 

     var po_ = unmarshalled; 

     var valid = validate(po_); 
     if (!valid) { 
      console.log('Validation failed.'); 
      console.log('Validation errors:'); 
      console.log(validate.errors); 
     } 


    }); 

を使用する結果は、次のようになります

Validation failed. 
    Validation errors: 
    [ { keyword: 'type', 
     dataPath: '.value.items.item[1].shipDate.timezone', 
     schemaPath: '#/definitions/integer/type', 
     params: { type: 'integer,null' }, 
     message: 'should be integer,null' }, 
     { keyword: 'type', 
     dataPath: '.value.items.item[1].shipDate', 
     schemaPath: '#/anyOf/1/type', 
     params: { type: 'null' }, 
     message: 'should be null' }, 
     { keyword: 'anyOf', 
     dataPath: '.value.items.item[1].shipDate', 
     schemaPath: '#/anyOf', 
     params: {}, 
     message: 'should match some schema in anyOf' }, 
     { keyword: 'enum', 
     dataPath: '.name.localPart', 
     schemaPath: '#/anyOf/1/properties/name/allOf/1/properties/localPart/enum', 
     params: { allowedValues: [Object] }, 
     message: 'should be equal to one of the allowed values' }, 
     { keyword: 'anyOf', 
     dataPath: '', 
     schemaPath: '#/anyOf', 
     params: {}, 
     message: 'should match some schema in anyOf' } ] 

しかし、これは私が再び不思議ますdataPath: '', ANをルートにエラー???

関連する問題