根据 NodeJS 中的模式 (xsd) 验证 XML

编程入门 行业动态 更新时间:2024-10-15 08:25:25
本文介绍了根据 NodeJS 中的模式 (xsd) 验证 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

NPM 中的任何 XML 库是否支持针对 XSD 架构验证 XML?

Do any of the XML libraries in NPM support the validation of XML against an XSD schema?

我会看自己,但是:

$ npm search xml 2>/dev/null | wc -l
212

注意:xsd 包不是看起来的那样,node-xerces 已损坏/为空.

Note: the xsd package is not what it seems and node-xerces is broken/empty.

推荐答案

Hij1nx (Paolo Fragomeni) 将我指向 https://github/polotek/libxmljs

Hij1nx (Paolo Fragomeni) pointed me at https://github/polotek/libxmljs

在试图弄清楚 API 半小时后,我有了一个解决方案:

After half an hour of trying to figure out the API, I have a solution:

#!/usr/local/bin/node
var x = require('libxmljs');

var xsd = '<?xml version="1.0" encoding="utf-8" ?><xs:schema xmlns:xs="http://www.w3/2001/XMLSchema" xmlns="http://example/XMLSchema/1.0" targetNamespace="http://example/XMLSchema/1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"><xs:element name="foo"></xs:element></xs:schema>'
var xsdDoc = x.parseXmlString(xsd);

var xml0 = '<?xml version="1.0" encoding="UTF-8"?><foo xmlns="http://example/XMLSchema/1.0"></foo>';
var xmlDoc0 = x.parseXmlString(xml0);
var xml1 = '<?xml version="1.0" encoding="UTF-8"?><bar xmlns="http://example/XMLSchema/1.0"></bar>';
var xmlDoc1 = x.parseXmlString(xml1);

var result0 = xmlDoc0.validate(xsdDoc);
console.log("result0:", result0);

var result1 = xmlDoc1.validate(xsdDoc);
console.log("result1:", result1);

这会产生输出:

result0: true
result1: false

我不知道这是否/如何与通过 URI 引用其他模式的模式一起工作.

I have no idea whether/how this will work with schemas which reference other schemas via URIs.

这篇关于根据 NodeJS 中的模式 (xsd) 验证 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 07:25:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1058709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   NodeJS   XML   xsd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!