首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSONIX如何进行强大的XML验证

JSONIX如何进行强大的XML验证
EN

Stack Overflow用户
提问于 2016-12-07 13:48:29
回答 1查看 420关注 0票数 0

我只是试着运行JSONIX示例采购订单。我做到了,就像高分WebSite上提到的那样。让我感到奇怪的是,这个示例基于XSD,传入XML的验证是用于具有子节点的元素,而不是简单的标记。

这将显示一个错误:

代码语言:javascript
复制
 ... <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>

这不是:

代码语言:javascript
复制
 ... <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>

那么,是否可以打开一个强验证,因为<quantity_will_cause_error>不是一个有效的元素。

亲切的问候

马库斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-07 15:01:41

现在我用这个

代码语言:javascript
复制
 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);
        }


    });

结果如下:

代码语言:javascript
复制
    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: '',根上的错误?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41019158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档