tags:

views:

179

answers:

4

Do you know of a tool that will derive a DTD (or other XML structure specification format) from a sample set of XML files?

Currently the only (automatic) validation we have for an xml encoded DSL is a legacy parser written in Perl, but for consistency reasons all perl code must be ported to C-sharp.

+2  A: 

http://www.stylusstudio.com/dtd%5Fgenerator.html is actual software implementing a DTD generator.

http://www.pmg.csail.mit.edu/~chmoh/pubs/wecwis.pdf seems like a nice paper on the kind of thing you'd need, but I can't find (links to) actual code anywhere in the paper so far.

Here's another paper on this, again, no code to be found: http://www.softnet.tuc.gr/~minos/Papers/debull03.pdf.

Finally, I'd also suggest you look into using RELAX NG or Schematron to validate your XML instead. Those languages are much more expressive, making them easier to read and more powerful in the kinds of things you can validate. (Be sure to skip XML Schema, which is widely considered to be a mess.)

djc
Interesting document! Too bad they never released the code (in a way that I can find :-)
lexu
http://www.hitsw.com/xml_utilites/ promises to create a DTD/schema from a single XML file ..
lexu
RE - RELAX NG or Schematron: I'll see if validators exist for the dotNet environment. thanks for the tips!
lexu
Nice find, those xml_utilities look good.
djc
A: 

Altova's XMLSpy has a DTD/XML Schema generator.

The generated DTD/XML Schema usually requires a little tweaking. For example, the tool may enumerate a list of attributes or elements, when you "meant" for it to allow any value. You're only giving it a sample of your problem space, and it has to go from specific to general, though. For that reason, I don't get too bent out of shape when it fails to read my mind.

I consider the generated dtd or schema a starting point. It's better than rolling it by hand from zero. Er, if you're starting with existing XML documents, that is.

Even if you're not going to use the generated dtd, it's a pretty good way to get your head around the structure of a set of unfamiliar XML documents.

Stephen Harmon
A: 

XMLMax editor will create an XSD from an XML file. The free trial(no registraton/small download file) will do this for you. If you want to do this in code, .NET framework has an XmlSchemaInference class that automatically creates an XSD from an xml file.

bill seacham
+2  A: 

You can use xsd.exe (part of visual studio) to generate an XML schema for a given XML file.

Vinz