使用XSD验证XML数据,而不使用Soap Envelope。

huangapple go评论67阅读模式
英文:

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 &lt;xsl:template match=&quot;*:MsgHeader&quot;&gt; rather than &lt;xsl:template match=&quot;*:Envelope&quot;&gt;

答案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(&#39;schema.xsd&#39;)));
SchemaValidator sv = sm.newSchemaValidator();
DocumentBuilder db = p.newDocumentBuilder();
XdmNode doc = db.build(new StreamSource(new File(&#39;source.xml&#39;)));
XdmNode target = (XdmNode)doc.select(descendant(&quot;payload&quot;)).findFirst().get();
sv.validate(target.asSource());

huangapple
  • 本文由 发表于 2020年9月29日 16:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64115294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定