tags:

views:

422

answers:

2

I know it is possible to generate skeleton XSD from XML. For example this post has good answers.

The question is how to generate XSD based on several XMLs. The idea is that each XML might have several different occurences of optional, arrays, choice and the like. From all those examples, I would like to compose the most accurate XSD.

I know there might be collisions and the like but assuming all the XML came from an uknown XSD, it should be theoreticaly possible. But is there such tool?

Thanks

+4  A: 

Trang is just such a tool written in by the notable James Clark. It can translate between different forms of xml definitions such as Relax NG normal and compact syntax, old school DTD and XML schema. It can also infer schema from one or more xml files.

If you run ubuntu trang is packaged in the universe repository but that version seems a bit broken and a clean download from the link above is probably your best option. Assuming trang.jar is in the current directory:

java -jar trang.jar -I xml -O xsd file1.xml file2.xml definition.xsd

should do what you want.

Knut Haugen
A: 

This is the code to create schema from one XML: Sample of code demonstrating how to use this class (it assumes that there is the “XmlSchemaSet set” class member accumulating the results and refining them from call to call):

        var si = new XmlSchemaInference();
        var reader = XmlReader.Create(new StringReader(textBox1.Text));
        var en = si.InferSchema(reader, set).Schemas().GetEnumerator();
        en.MoveNext();
        var schema = en.Current as XmlSchema;
        var stream = new MemoryStream();
        if (schema != null)
        {
            schema.Write(stream);
            set.Add(schema);
        }
        stream.Flush();
        stream.Position = 0;
        var streamReader = new StreamReader(stream);
        var str = streamReader.ReadToEnd();
        grid1.Model.LoadSchema(str);
        reader.Close();
        stream.Close();
        streamReader.Close();

If you run it again and give the XMLSchemaInference the generated schema and another XML, it will enhance the schema

Ragoler