How to generate XSD from XML elements

I have an XML input

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

I am looking for a library or tool that will take an XML instance document and output the appropriate XSD schema.

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

+7
source share
5 answers

If all you need is an XSD so that the corresponding XML code matches it, you would be much better off creating it yourself rather than using the tool.

No one knows better than you schema features such as valid values ​​(for example, is the <maxlength> element? true and false only valid values ​​for <required> ?).

If you really want to use the tool (I would advise you to use it if you have not developed XML and really can not get a real XSD, or if you developed it, double check the generated XSD) you can try Trang . It can infer the XSD schema from a series of XML examples.

You will need to take into account that the XSD tool you can do may be incomplete or inaccurate if the XML samples are not representative enough.

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

Here you can find an example of using Trang here .

+9
source

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

+5
source

You can write XSLT to do something like this. But the problem is that one document is not enough to create a diagram. Are any of these elements optional? Is there anything in this document that may appear in other cases? How much can a certain item be? Should they be in that order? There are many things that can be expressed in a scheme that are not immediately obvious from a single copy of the document that corresponds to this scheme.

+1
source

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

+1
source

Try xmlbeans, it has tools, one of which is ins2xsd, you can find the specifics here: http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html Good luck.

0
source

All Articles