# XSD generator

Source: https://xmlvalid.com/tools/xsd-generator

> Infer an XML Schema from one or more sample documents: element order and counts, required and optional attributes, value types and namespaces. The samples are then validated against the result, so you know it accepts them. Everything runs in your browser.

## How the schema is inferred

- **Order and counts.** Children keep the order the samples agree on, in an `xs:sequence`. A child missing from some samples gets `minOccurs="0"`, one that repeats `maxOccurs="unbounded"`. When samples disagree on the order, the content becomes a repeated `xs:choice`.
- **Types.** Values that are all integers become `xs:integer`, then `xs:decimal`, `xs:boolean`, `xs:date`, `xs:dateTime` and `xs:time`; anything else is `xs:string`. Codes with leading zeros (`0042`) stay strings. Turn off **Infer types** to type everything as a string.
- **Attributes.** One present on every occurrence is `use="required"`.
- **Namespaces.** Each namespace gets its own schema file, imported by the main one, with the prefixes the document uses.

More samples give a better schema: add several documents and everything they have in common, and everything they don't, is taken into account.

## Russian doll, Venetian blind, Salami slice

**Russian doll**

One global element, everything else declared inside it. The schema reads like the document. Elements with the same name in different places get their own declarations.

**Venetian blind**

Local elements with named complex types (`PartyType`), defined at the top level, so other schemas can reuse them.

**Salami slice**

Every element global, referenced with `ref`. Elements with the same name share one declaration wherever they appear.

## After generating

An inferred schema describes your samples, not your intent: tighten it by hand where you know more, with enumerations, patterns and lengths. Then validate new documents against it in the [validator](https://xmlvalid.com/validate) or from code. If the format is a published standard (UBL, CII, ISO 20022), use its official schema from the [catalog](https://xmlvalid.com/schemas) instead.

## Questions

### Is my XML uploaded?

No. The schema is inferred and the samples are validated in your browser.

### Can it use several sample documents?

Yes, and it should: add files next to the pasted document, and the schema accepts all of them.

### Which XSD version does it write?

XML Schema 1.0, which every validator reads.

### What about DTD or RelaxNG?

It writes XSD only. The validator accepts all three if you already have a DTD or a RelaxNG schema.

### Other tools

- [XML formatter](https://xmlvalid.com/tools/xml-formatter): Pretty-print or minify XML, keeping text, CDATA and comments as written.
- [XPath tester](https://xmlvalid.com/tools/xpath-tester): Evaluate XPath 3.1 against a document and see each match with its line.
- [XML from XSD](https://xmlvalid.com/tools/xsd-to-xml): Generate a sample XML document from a schema, validated against it.
- [XSLT transformer](https://xmlvalid.com/tools/xslt-transformer): Run XSLT 1.0, 2.0 or 3.0 on a document and see the result, the messages and the files it writes.
- [XML diff](https://xmlvalid.com/tools/xml-diff): Compare two XML documents as trees: changed, added and removed elements, attributes and text.
- [XML to JSON converter](https://xmlvalid.com/tools/xml-to-json): Convert XML to JSON, with arrays where an element repeats or wherever its XSD allows.
- [JSON to XML converter](https://xmlvalid.com/tools/json-to-xml): Convert JSON to XML: @keys become attributes, arrays repeated elements.
- [XML to CSV converter](https://xmlvalid.com/tools/xml-to-csv): Turn a repeating element into CSV rows: invoice lines, products, records.
- [E-invoice viewer](https://xmlvalid.com/tools/e-invoice-viewer): Read an XRechnung, ZUGFeRD, Factur-X or Peppol invoice as a document, and print it.
- [XML validator](https://xmlvalid.com/validate): check a document against XSD, DTD, RelaxNG or Schematron, up to 1 GB.
