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.

0

10 Answers 10

68

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

7
  • 1
    wow, months later you pointed me to a great solution to do a quick XDR to XSD conversion! Thanks! Feb 25, 2009 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 3, 2012 at 6:04
  • 2
    In case someone doesn't have Visual Studio installed, here is an alternative link to download xsd: juliankay.com/development/download-xsd-exe
    – M.D.
    May 8, 2012 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. Nov 8, 2012 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? Apr 15, 2013 at 22:25
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).

8
  • 3
    OMG This is so much better than all that Powershell bullshit! Especially since I didn't have a XSD file!
    – Chloe
    Jul 6, 2012 at 16:14
  • The free community version of IntelliJ has such functionality, too: jetbrains.com/idea/webhelp/…
    – Nick
    Sep 25, 2013 at 12:02
  • 3
    worked like a charm :: cd <xml_bean_bin_directory> ; ./inst2xsd -design ss -simple-content-types smart -enumerations 4 <input_file.xml> -outDir <output_Directory>
    – diaryfolio
    Sep 10, 2014 at 11:22
  • 1
  • 4
    Note that XMLBeans has been moved to attic.apache.org, the home of retired Apache projects. In other words, it has not been updated since 2012.
    – Magnilex
    Sep 1, 2015 at 7:49
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

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 4, 2010 at 9:30
  • 5
    If all you need is generate xsd from xml file, trang is the best, fastest and easiest IMHO.
    – kriver
    Jul 17, 2012 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, 2013 at 20:56
  • 1
    on windows run: java -jar trang.jar file.xml schema.xsd Aug 1, 2016 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! Nov 4, 2016 at 15:31
19

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

1
  • 3
    And in higher versions Aug 14, 2015 at 16:39
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.

4
  • 1
    Be aware this tool is written in php, so it's not trivial to setup.
    – 79E09796
    Mar 19, 2012 at 9: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, 2012 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, 2012 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, 2018 at 7: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

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/

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.

1
  • 2
    And this is also available in the free community edition of Idea.
    – lbalazscs
    Nov 15, 2013 at 10:31
4

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.