Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate XSD from elements of XML

Tags:

java

xml

xsd

I have a XML input

<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.

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

like image 636
Ankit Khemani Avatar asked May 07 '13 13:05

Ankit Khemani


People also ask

Is it possible to generate XSD from XML?

The XML editor lets you create an XML Schema definition language (XSD) schema from an XML document.

How do I convert XML to XSD?

How to generate/create a schema xsd from an XML document? Step 1: click Open File button and select the xml file from the file system that you have access, or get the xml file from internet via URL, click By URL. Step 2: click the Generate XSD button, the generated schema will be displayed in an indented XML format.

How do I create an XSD file?

In Visual Studio, open the File menu and select New > File. Or, use the Ctrl+N keyboard shortcut. In the New File dialog box, select XML Schema and then select Open. A new file is created.


1 Answers

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.

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 true and false the only valid values for <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.

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.

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

You can find a usage example of Trang here.

like image 182
Xavi López Avatar answered Oct 10 '22 19:10

Xavi López