Java 如何从 XML 元素生成 XSD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16420241/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:50:55  来源:igfitidea点击:

How to generate XSD from elements of XML

javaxmlxsd

提问by Ankit Khemani

I have a XML input

我有一个 XML 输入

<field>
  <name>id</name>
  <dataType>string</dataType>
  <maxlength>42</maxlength>
  <required>false</required>
</field>

I am looking for a library or a tool which will take an XML instance document and output a corresponding XSD schema.

我正在寻找一个库或一个工具,它将采用 XML 实例文档并输出相应的 XSD 模式。

I am looking for some java library with which I can generate a XSD for the above XML structure

我正在寻找一些可以为上述 XML 结构生成 XSD 的 java 库

回答by Michal Borek

You can try with online tool called XMLGrid: http://xmlgrid.net/xml2xsd.html

您可以尝试使用名为 XMLGrid 的在线工具:http://xmlgrid.net/xml2xsd.html

回答by Georgemc

You could write an XSLT to do somethinglike that. But the problem is, a single document alone is not enough information to generate a schema. Are any of those elements optional? Is there anything missing from that document, that might appear in other instances? How many of a particular element can there be? Do they have to be in that order? There are loads of things that can be expressed in a schema, that are not immediately obvious from one instance of a document that conforms to that schema.

您可以编写一个 XSLT 来做类似的事情。但问题是,单独的单个文档不足以生成模式的信息。这些元素中的任何一个是可选的吗?该文件中是否缺少任何可能出现在其他情况下的内容?一个特定的元素可以有多少个?他们必须按照这个顺序吗?有很多东西可以在模式中表达,但从符合该模式的文档的一个实例中并不能立即明显看出。

回答by Xavi López

If all you want is an XSD so that the XML you gave conforms to it, you'd be much better off by crafting it yourself rather than using a tool.

如果您想要的只是一个 XSD,以便您提供的 XML 符合它,那么您最好自己制作它而不是使用工具。

No one knows better than you the particularities of the schema, such as which valid values are there (for instance, is the <maxlength>element required? are trueand falsethe only valid values for <required>?).

没有人比您更了解模式的特殊性,例如存在哪些有效值(例如,<maxlength>元素是必需的吗?是true以及?false的唯一有效值<required>)。

If you really want to use a tool (I'd only advice using it if you haven't designed the XML and really can't get the real XSD - or if you designed it, double check the generated XSD), you could try Trang. It can infer an XSD Schema from a number of example XML's.

如果你真的想使用一个工具(如果你没有设计 XML 并且真的无法获得真正的 XSD,我只建议使用它 - 或者如果你设计了它,请仔细检查生成的 XSD),你可以尝试。它可以从许多示例 XML 中推断出 XSD 模式。

You'll have to take into account that the XSD a tool can infer you might be incomplete or inaccurate if XML samples aren't representative enough.

您必须考虑到,如果 XML 样本的代表性不够,工具可以推断出的 XSD 可能不完整或不准确。

java -jar trang.jar sampleXML.xml inferredXSD.xsd

You can find a usage example of Trang here.

您可以在此处找到Trang使用示例

回答by Tom

Try xmlbeans it has some tools one of them is ins2xsd you can find specifics here: http://xmlbeans.apache.org/docs/2.0.0/guide/tools.htmlGood luck

试试 xmlbeans 它有一些工具,其中之一是 ins2xsd 你可以在这里找到细节:http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html 祝你 好运

回答by Kay Tsar

For the people who really want to include it in their Java code to generate an XSD and understand the perils, check out Generate XSD from XML programatically in Java

对于真正想将它包含在他们的 Java 代码中以生成 XSD 并了解危险的人,请查看在 Java 中以编程方式从 XML 生成 XSD