151

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

I certainly recognize that the generated XSD schema will be limited when compared to creating a schema by hand (it probably won't handle optional or repeating elements, or data constraints), but it could at least serve as a quick starting point.

Linus Fernandes
  • 498
  • 5
  • 30
Matt Dillard
  • 14,677
  • 7
  • 51
  • 61

10 Answers10

69

the Microsoft XSD inference tool is a good, free solution. Many XML editing tools, such as XmlSpy (mentioned by @Garth Gilmour) or OxygenXML Editor also have that feature. They're rather expensive, though. BizTalk Server also has an XSD inferring tool as well.

edit: I just discovered the .net XmlSchemaInference class, so if you're using .net you should consider that

Sachin Joseph
  • 18,928
  • 4
  • 42
  • 62
Danimal
  • 7,672
  • 8
  • 47
  • 57
  • 1
    wow, months later you pointed me to a great solution to do a quick XDR to XSD conversion! Thanks! – bethlakshmi Feb 25 '09 at 22:55
  • 6
    It was hard for me to find a download for this tool (xsd.exe), but it was already on my system in C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin. Just thought I would share in case that helps anybody. – oob Jan 03 '12 at 06:04
  • 2
    In case someone doesn't have Visual Studio installed, here is an alternative link to download xsd: http://juliankay.com/development/download-xsd-exe/ – M.D. May 08 '12 at 18:40
  • 5
    The directory seems `xsd.exe` to have changed on Windows 7. I have it in many `Microsoft SDKs` directories (so it does not seem to necessarily depend on Visual Studio anymore?), among which this one: `C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools`. I had to run it in the directory containing the xml, to prevent an error about "illegal characters in path", but that may be due to the long filename I have. – Yahoo Serious Nov 08 '12 at 15:01
  • Anyone else having trouble using MS XSD inference tool, raising an OutOfMemoryException for a 200MB XML file with still more than 8GB free RAM? – Felipe Sabino Apr 15 '13 at 22:25
  • @Felipe Sabino: I seem to recall some trouble (but it is 'already' 6 months ago). Looking at the directory, I used excerpts of the large XML, with only one child node per example. (I am trying to remember if `xsd.exe` only used the first instance per node.) I think I used multiple examples, to generate multiple `.xsd`'s, and then manually merged the results. After that I manually added more optional nodes (and relationships, and data types) by validating the large XML-file, see this [MSDN validating example](http://msdn.microsoft.com/en-us/library/ms171806%28v=sql.100%29.aspx). – Yahoo Serious Apr 29 '13 at 14:34
  • in 2021, i could find the xsd.exe on my Windows-10 by searching within here `C:\Program Files (x86)\Microsoft SDKs\Windows` ! – joedotnot Mar 14 '21 at 14:40
60

You can use an open source and cross-platform option: inst2xsd from Apache's XMLBeans. I find it very useful and easy.

Just download, unzip and play (it requires Java).

Dario
  • 931
  • 8
  • 18
  • 3
    OMG This is so much better than all that Powershell bullshit! Especially since I didn't have a XSD file! – Chloe Jul 06 '12 at 16:14
  • The free community version of IntelliJ has such functionality, too: http://www.jetbrains.com/idea/webhelp/generating-xml-schema-from-instance-document.html – Nick Sep 25 '13 at 12:02
  • 3
    worked like a charm :: cd ; ./inst2xsd -design ss -simple-content-types smart -enumerations 4 -outDir – diaryfolio Sep 10 '14 at 11:22
  • 1
    https://archive.apache.org/dist/xmlbeans/binaries/ – magnetik Nov 27 '14 at 08:50
  • 4
    Note that XMLBeans has been moved to http://attic.apache.org/, the home of retired Apache projects. In other words, it has not been updated since 2012. – Magnilex Sep 01 '15 at 07:49
  • @Magnilex - while it has been archived - I still end up having to use it quite frequently as JAXB often isn't up to the job - thanks Dario and diaryfolio - it worked for me – JGlass Mar 27 '18 at 15:11
  • It's back to life at http://xmlbeans.apache.org/ – TheTFo Nov 26 '18 at 23:00
  • -enumerations option is not working anymore, at least in the version packaged in ubuntu20.04. The option should be used as -enumerations never, or the elements "value" child element of xs:enumeration should be converted to one attribute, like this: xs:enumeration value="myenumvalue" – yucer Apr 27 '21 at 09:56
56

Trang is the best option here. Open source and cross platform (although Java is required)

From the Trang Website:

Trang converts between different schema languages for XML. It supports the following languages

  • RELAX NG (XML syntax)
  • RELAX NG compact syntax
  • XML 1.0 DTDs
  • W3C XML Schema

A schema written in any of the supported schema languages can be converted into any of the other supported schema languages, except that W3C XML Schema is supported for output only, not for input.

Trang can also infer a schema from one or more example XML documents.

Download Link

Community
  • 1
  • 1
Derferman
  • 717
  • 5
  • 5
  • 1
    Trang is also the tool integrated into OxygenXML to do its XSD inference from one or more XMl docs. At first attempt here it seems to work well. – redcalx Jun 04 '10 at 09:30
  • 5
    If all you need is generate xsd from xml file, trang is the best, fastest and easiest IMHO. – kriver Jul 17 '12 at 22:54
  • 3
    The trang documentation is a little thin. To generate a schema, run `$ trang file.xml schema.xsd`. – Joe Mornin Dec 31 '13 at 20:56
  • 1
    on windows run: java -jar trang.jar file.xml schema.xsd – Alexey Obukhov Aug 01 '16 at 12:04
  • 1
    Had substructures which appear in the same form in various places in my XML sample, Trang did a perfect job in detecting the repetition and generating a minimal XSD, which is what I needed. Very nice! – Tomislav Nakic-Alfirevic Nov 04 '16 at 15:31
19

In VS2010 if you load an XML file into the editor, click the XML menu >> Create Schema.

Nathan Donnellan
  • 573
  • 3
  • 11
13

There also is XML schema learner which is available on Github.

It can take multiple xml files and extract a common XSD from all of those files.

edorian
  • 38,542
  • 15
  • 125
  • 143
  • 1
    Be aware this tool is written in php, so it's not trivial to setup. – 79E09796 Mar 19 '12 at 09:55
  • 5
    @79E09796 Installing a cli php with `sudo apt-get install php5-cli` and running `./learn --help` --- I don't really see the difference between that and a tool written in java, python or ruby :) – edorian Mar 19 '12 at 11:46
  • 1
    just tried under windows -- worked great with php 5.4 without any installation. Both php and XML Schema learner just unzipped and executed using the full paths ;) – vak May 22 '12 at 11:52
  • small comment on the `locality` field: if a field name is used in multiple places in the XML referring to *different* elements, a larger `locality` value than 1 is required, e.g. `root/item` and `root/something/item` refer to different types, then `--locality 2` is required as a minimum – Shadi Jan 23 '18 at 07:35
11

If you have .Net installed, a tool to generate XSD schemas and classes is already included by default.
For me, the XSD tool is installed under the following structure. This may differ depending on your installation directory.

C:\Program Files\Microsoft Visual Studio 8\VC>xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.

xsd.exe -
   Utility to generate schema or class files from given source.

xsd.exe <schema>.xsd /classes|dataset [/e:] [/l:] [/n:] [/o:] [/s] [/uri:]
xsd.exe <assembly>.dll|.exe [/outputdir:] [/type: [...]]
xsd.exe <instance>.xml [/outputdir:]
xsd.exe <schema>.xdr [/outputdir:]

Normally the classes and schemas that this tool generates work rather well, especially if you're going to be consuming them in a .Net language

I typically take the XML document that I'm after, push it through the XSD tool with the /o:<your path> flag to generate a schema (xsd) and then push the xsd file back through the tool using the /classes /L:VB (or CS) /o:<your path> flags to get classes that I can import and use in my day to day .Net projects

Pat Hermens
  • 906
  • 6
  • 19
7

If all you want is XSD, LiquidXML has a free version that does XSDs, and its got a GUI to it so you can tweak the XSD if you like. Anyways nowadays I write my own XSDs by hand, but its all thanks to this app.

http://www.liquid-technologies.com/

Robert Gould
  • 68,773
  • 61
  • 187
  • 272
5

if you are working in the java world - intelliJ idea has also extensive xml support, including xsd generation and samle xml from xsd generation, and with plugins you can get xslt debuggers. - especially nice if you plan to use tools such as jaxb afterwards.

Andreas Petersson
  • 16,248
  • 11
  • 59
  • 91
4

This is an old thread but I thought it could be useful to post this link: just found this tool: xsd-gen Generate XML Schema from XML

and it just did what I needed.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
v_e
  • 377
  • 1
  • 9
4

Altova XmlSpy does this well - you can find an overview here

Garth Gilmour
  • 11,124
  • 5
  • 25
  • 35