XML to XSD
XML to XSD
Paste a representative document and get a schema that validates it. This is the mirror of XSD to sample XML, and the honest framing is that it is a starting point: one sample cannot tell you the full contract, so the tool is explicit about which parts it observed and which it assumed. Read the status line — it counts what was inferred.
What can and cannot be inferred from one document
A schema states what is allowed. A document only shows what occurred. Everything interesting about generating one from the other lives in that gap, so here is exactly which way each guess is made:
- Repeating elements.
maxOccurs="unbounded"is only asserted when a repeat was actually seen — two or more siblings of the same name inside one parent. A list with a single entry in your sample producesmaxOccurs="1", which is almost certainly too tight; that is the first thing to widen by hand. - Optional elements and attributes.
minOccurs="0"anduse="optional"are asserted when some instance of the parent had the child and another did not. With only one instance, everything present looks required. - Types widen, never narrow. A value is typed as the narrowest built-in every observed value satisfies, checked in order: boolean, integer, decimal, dateTime, date, time, anyURI, then string. One non-numeric value among a thousand numbers makes the whole element
xs:string, which is the safe direction to be wrong in. - Child order.
xs:sequenceis a claim that children always appear in that order. If two instances of the same parent listed their children differently, that claim is false, so the model becomesxs:allinstead and the status line says how many elements it happened to. Override it if you know the real rule. - Mixed content. An element with both child elements and its own non-whitespace text gets
mixed="true", because without it a validator rejects the text. - Attributes on text-only elements need
xs:simpleContentwith anxs:extension— a detail that is easy to get wrong by hand and produces a schema that silently rejects your document.
Nested or flat
Nested uses anonymous inline types, so the schema reads in the same shape as the document. It is the easier one to check against a sample by eye, and the right default.
Flat declares every element globally and refers to them by ref. That
is the shape you want if the schema will be maintained by hand or imported elsewhere, because each
element is defined exactly once — but it also means two elements that happen to share a name must
share a definition, so their models are merged.
Either way, an element name that appears in several places is modelled once from every occurrence
combined. If your document uses <item> for two genuinely different things, the
inferred type is the union of both and will be looser than either. Rename one, or split the schema.
Namespaces
If the sample's root is in a namespace, that becomes the targetNamespace and
elementFormDefault="qualified" is set, which is what makes an unprefixed child element
in your document validate. A document in no namespace produces a no-namespace schema. Multiple
namespaces in one document are beyond what a single inferred schema can express — the first is used
and the rest are kept as literal prefixed names, which will need an xs:import adding
by hand.
FAQ
Can I trust the output as-is?
Only as far as your sample is representative. Validate a few other real documents against it with the XSD validator — that is the fastest way to find where the sample was too narrow, and it is a two-minute check that saves a bad schema from shipping.
Why xs:all rather than xs:choice when the order varies?
Because varying order is evidence about ordering, not about alternation. xs:choice says exactly one of these appears, which is a much stronger claim and one a sample showing several children together actually contradicts. xs:all says these appear, in any order — which is precisely what was observed.
Does it handle comments, CDATA and processing instructions?
Comments and processing instructions carry no schema information and are ignored. CDATA is treated as text, which is what it is — so an element whose value arrives in a CDATA section is typed from that text like any other.
Is my document uploaded?
No. Parsing uses the browser's own XML parser and the schema is built in the page. Nothing is sent anywhere.