xml 验证 XSD 架构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1199950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate an XSD Schema?
提问by Alexander Kj?ll
I'm writing an XML schema (an XSD) to describe the format our partners should send us data in.
我正在编写一个 XML 模式(一个 XSD)来描述我们的合作伙伴应该向我们发送数据的格式。
And I'm having a hard time finding a tool that can validate the XSD schema file that I have written.
而且我很难找到可以验证我编写的 XSD 架构文件的工具。
The best way I have found so far is to first write an example input XML file and then try to validate thatwith the XSD. But that doesn't feel like a best practice maneuver.
到目前为止,我已经找到了最好的办法是先写一个例子输入XML文件,然后试图验证是与XSD。但这并不像是最佳实践操作。
So, how should I validate an XML schema?
那么,我应该如何验证 XML 模式?
采纳答案by lavinio
If this is a short-term thing, you could use an evaluation copy of a tool like Stylus Studio.
如果这是短期的事情,您可以使用Stylus Studio等工具的评估副本。
If it's long-term maintenance, you might want to consider purchasing an XML schema editor like Stylus, or Oxygenor Altova.
如果是长期维护,您可能需要考虑购买像 Stylus、Oxygen或Altova这样的 XML 模式编辑器。
You didn't specify the source language, but it's only a few lines of code to write a schema validator in Java or .Net.
您没有指定源语言,但是用 Java 或 .Net 编写模式验证器只需几行代码。
回答by Matt Lavin
The W3C has a online validator for XML Schemas at http://www.w3.org/2001/03/webdata/xsv. Since W3C is the source of the XML Schema spec, their validator should be trustworthy.
W3C 在http://www.w3.org/2001/03/webdata/xsv 上有一个 XML 模式的在线验证器。由于 W3C 是 XML Schema 规范的来源,因此它们的验证器应该是值得信赖的。
回答by Pierre
the Java SDK comes with a standard tool called xjc. This tool generates the classes parsing your schema. You could use this code to validate your partners' input.
Java SDK 附带一个名为xjc的标准工具。此工具生成解析您的架构的类。您可以使用此代码来验证合作伙伴的输入。
See also : http://www.java2s.com/Tutorial/Java/0440__XML/ThexjcTool.htm
另见:http: //www.java2s.com/Tutorial/Java/0440__XML/ThexjcTool.htm
回答by HankCa
cd /the/dir/with/your/schema
curl -O https://www.w3.org/2012/04/XMLSchema.xsd
xmllint.exe --noout --schema XMLSchema.xsd <your schema>
In *nix (including git-bash or similar on Windows), if the schema is valid then $? == 0else $? == 1. I'm sure there is some powershell equivalent ...
在 *nix(包括 Windows 上的 git-bash 或类似内容)中,如果架构有效,则$? == 0else $? == 1。我确定有一些等效的 powershell ......
This came from a comment by @AlexanderKj?ll to an answer by @lavinio elsewhere here. I added my own comment to say @AlexanderKj?ll should add this as an answer. However (for me atleast), it wasn't quite correct since it won't work using the remote file URI. And thus my answer. If you upvote this could you please upvote their comment.
这来自@AlexanderKj?ll 的评论对@lavinio 在这里其他地方的回答。我添加了我自己的评论说@AlexanderKj?ll 应该添加这个作为答案。但是(至少对我而言),它不太正确,因为它无法使用远程文件 URI 工作。因此我的回答。如果您对此赞不绝口,请支持他们的评论。
回答by DavidRR
So, how should I validate an XML schema (XSD)?
那么,我应该如何验证 XML 模式 (XSD)?
The question you ask is a good one. Just "winging it" is likely to get you in trouble. From the Wikipedia article XML Schema Editor(emphasis mine):
你问的问题很好。只是“随心所欲”可能会给您带来麻烦。来自维基百科文章XML 模式编辑器(重点是我的):
The [W3C XML Schema] standard is versatile, allowing for programming concepts such as inheritance and type creation, but it is complex. The standard itself is highly technicaland published in 3 different parts, making it difficult to understand without committing large amounts of time.
[W3C XML Schema] 标准是通用的,允许继承和类型创建等编程概念,但它很复杂。 该标准本身技术性很强,分为 3 个不同的部分,如果不花大量时间就很难理解。
So, given the complexity of the W3C XML Schema standard, how do you ensure that a schema that you create complies with that standard? From the same Wikipedia article:
那么,考虑到 W3C XML 模式标准的复杂性,您如何确保您创建的模式符合该标准?来自同一篇维基百科文章:
The problems users face when working with the XSD standard can be mitigated with the use of graphical editing tools. Although any text-based editor can be used to edit an XML Schema, a graphical editor offers advantages; allowing the structure of the document to be viewed graphically and edited with validation support, entry helpers and other useful features.
用户在使用 XSD 标准时面临的问题可以通过使用图形编辑工具来缓解。尽管任何基于文本的编辑器都可用于编辑 XML 模式,但图形编辑器具有优势;允许以图形方式查看和编辑文档的结构,并使用验证支持、输入助手和其他有用的功能。
At the bottom of that Wikipedia article you'll find a list of XML schema editors, some of which are licensed as "free software."
在 Wikipedia 文章的底部,您会找到 XML 模式编辑器的列表,其中一些被授权为“免费软件”。

