XML Schema Generator

Paste an XML document and get a schema it satisfies. Element structure, attributes, types and how often each element may appear are all inferred from what is actually in the document.

XML

Read on this page. Nothing is uploaded.

XSD schema

Paste XML on the left and the inferred schema appears here.

What is inferred, and from what

Structure comes from the element tree: every child of an element becomes an entry in an xs:sequence, in the order the document has them.

Occurrence comes from counting. An element that appears more than once inside any single parent gets maxOccurs unbounded. An element that appears in some parents and not others gets minOccurs zero, which is the closest a single document can get to knowing something is optional.

Types come from the values. A value that is all digits becomes xs:integer, one with a decimal point becomes xs:decimal, true and false become xs:boolean, an ISO date becomes xs:date. A value with a leading zero stays xs:string, because it is an identifier rather than a number and typing it as an integer is how a schema starts rejecting good documents.

Where one element holds different kinds of value across the document, the type widens to whatever accepts all of them. Integers mixed with decimals become xs:decimal; anything mixed with text becomes xs:string.

A schema from one document is a guess

A schema describes every document of a kind. What you have is one example of that kind, so the schema describes your example and not necessarily the family it belongs to.

Two things are worth checking by hand every time. An optional element that happens to be present in your sample is inferred as required, because nothing in the document says otherwise. And a repeating element that appears exactly once is inferred as appearing exactly once.

Feeding a larger and more varied sample fixes both, and reading the result before you use it fixes the rest. Treat the output as a first draft with the tedious part already done.

Namespaces

Namespace prefixes are kept in element names as written, and no targetNamespace is declared. A document that uses namespaces properly needs that adding by hand, along with the matching xmlns declaration.

Questions

Will the generated schema validate my document?

Yes, that is the one thing it is guaranteed to do. It was derived from the document, so the document satisfies it. Whether it correctly describes the next document is the open question.

What is the difference between XSD and DTD?

XSD is written in XML, supports data types and namespaces, and can express how many times something may occur. DTD has its own syntax, no real type system and no namespace support. XSD replaced it for anything new.

Can I generate XML from a schema instead?

Not here. Going that way means inventing values that fit the types, and a document full of invented values is rarely what anyone wants.