英文:
Validate XML data with XSD without Soap Envelope
问题
我有带有SOAP信封的XML数据,但是我的XSD模式只能验证信封内部的XML数据。我正在寻找一种机制,可以编辑XSD并且遍历它,仅对信封内部的XML进行验证。
就像例如,在XSLT中,我们可以设置根参数<xsl:template match="*:MsgHeader">
,而不是<xsl:template match="*:Envelope">
。
英文:
I have XML data with SOAP Envelope but my XSD schemas can only validate XML data inside the envelope. I am looking forward to the mechanism in which i can edit XSD and traverse and only validate the xml which is inside the envelope.
Like for example, in XSLT we get option to set the root parameter <xsl:template match="*:MsgHeader">
rather than <xsl:template match="*:Envelope">
答案1
得分: 1
按你的建议,在XSLT(2.0+)中进行验证是一个选项,但可能不是一个很好的选项,因为XSLT会在第一个验证错误后停止。
如果你使用Saxon作为你的验证引擎,你可以像这样从Java中进行所需的验证:
Processor p = new Processor(true);
SchemaManager sm = p.getSchemaManager();
sm.load(new StreamSource(new File("schema.xsd")));
SchemaValidator sv = sm.newSchemaValidator();
DocumentBuilder db = p.newDocumentBuilder();
XdmNode doc = db.build(new StreamSource(new File("source.xml")));
XdmNode target = (XdmNode) doc.select(descendant("payload")).findFirst().get();
sv.validate(target.asSource());
英文:
As you suggest, doing the validation in XSLT (2.0+) is one option - but probably not a very good one, because XSLT stops after the first validation error.
If you use Saxon as your validation engine then you can do the required validation from Java like this:
Processor p = new Processor(true);
SchemaManager sm = p.getSchemaManager();
sm.load(new StreamSource(new File('schema.xsd')));
SchemaValidator sv = sm.newSchemaValidator();
DocumentBuilder db = p.newDocumentBuilder();
XdmNode doc = db.build(new StreamSource(new File('source.xml')));
XdmNode target = (XdmNode)doc.select(descendant("payload")).findFirst().get();
sv.validate(target.asSource());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论