---
title: "How to create an XML file from an XSD"
description: "Create a valid XML document from an XSD: read the schema's root element, order, and required parts by hand, or generate a starting document in XML Lens, Visual Studio, Eclipse, or IntelliJ."
author: "XML Lens editorial"
datePublished: 2026-11-22
dateModified: 2026-11-22
canonical: https://xmllens.ayushdev.com/guides/create-xml-from-xsd
---

# How to create an XML file from an XSD

To create an XML file from an XSD, start with a global element declared directly under xs:schema, put it in the schema's targetNamespace, then add every child in the order its xs:sequence lists, every element without minOccurs="0", and every attribute with use="required". Validate the result to confirm it. Several editors can generate this starting document for you, including XML Lens with New Document from Schema.

XML Lens editorial · Reviewed 2026-11-22

## 1. Find the root element and namespace

An XML document can start with any element declared globally, directly under xs:schema. Elements declared inside a complexType are local and cannot be the root. Most schemas have one obvious candidate; in the example below it is order.

Next, look at the schema's targetNamespace. If it has one, your document's elements must be in that namespace, so the root element needs xmlns="urn:example:orders". If elementFormDefault="qualified", as here, the child elements are in it too, which the default namespace takes care of.

```
<?xml version="1.0" encoding="UTF-8"?>
<!-- order.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:example:orders"
           xmlns="urn:example:orders"
           elementFormDefault="qualified">
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="customer" type="xs:string"/>
        <xs:element name="item" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="quantity" type="xs:positiveInteger"/>
              <xs:element name="price" type="xs:decimal"/>
            </xs:sequence>
            <xs:attribute name="sku" use="required">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:pattern value="[A-Z]{3}-[0-9]+"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="status" default="draft">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="draft"/>
            <xs:enumeration value="submitted"/>
            <xs:enumeration value="shipped"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>
```

## 2. Add what is required, in order

Walk the content model from the root down. In an xs:sequence, children must appear in the listed order. An element without minOccurs is required once; minOccurs="0" makes it optional, and maxOccurs="unbounded" lets it repeat. An xs:choice needs exactly one of its options; an xs:all allows its children in any order, each at most once.

Then add attributes. use="required" means the attribute must be present; others are optional, and a default value applies when they are left out. Finally fill in values that match each type: a positive whole number for xs:positiveInteger, a decimal for xs:decimal, and anything matching the pattern or enumeration of a restricted type.

- order needs one customer, then at least one item
- each item needs a sku attribute matching [A-Z]{3}-[0-9]+
- each item needs name, quantity, and price, in that order
- status is optional and defaults to draft

## 3. The finished document

Here is the smallest document that satisfies order.xsd, plus a schemaLocation hint so editors can find the schema. Save it beside order.xsd as order.xml.

```
<?xml version="1.0" encoding="UTF-8"?>
<order xmlns="urn:example:orders"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="urn:example:orders order.xsd"
       status="submitted">
  <customer>Ada Lovelace</customer>
  <item sku="LNS-1">
    <name>XML Lens Pro</name>
    <quantity>1</quantity>
    <price>29.99</price>
  </item>
</order>
```

## 4. Validate it

A document you wrote by hand is only valid once a validator agrees. On a Mac, xmllint is already installed. A valid file prints one line ending in validates; anything else prints the line and rule that failed.

```
$ xmllint --noout --schema order.xsd order.xml
order.xml validates
```

## 5. Generate a starting document instead

For a large schema, writing the skeleton by hand is slow. Several editors generate a starting document from a chosen root element. Generated documents use placeholder values, so review them and validate before use.

- XML Lens (Mac): Schema menu > New Document from Schema, choose the root element and Required only or Everything. The new document opens with the schema attached and checked as you type.
- Visual Studio (Windows): in XML Schema Explorer, right-click a global element and choose Generate Sample XML.
- Eclipse: File > New > Other > XML File, then choose to create it from an XML schema file and pick the root element.
- IntelliJ IDEA: Tools > XML Actions > Generate XML Document from XML Schema.

## 6. Fill it in with schema-aware completion

Once the skeleton exists, a schema-aware editor keeps you valid while you add real data. In XML Lens, type < where you want a new element and the list shows only what the schema allows there; inside a tag a space lists the attributes; in a value with an enumeration the list shows the allowed values. Choosing an element inserts its required attributes, and Tab moves to the next empty field.

```
<order xmlns="urn:example:orders">
  <customer>Ada Lovelace</customer>
  <item sku="LNS-1">…</item>
  <|          ← type <: item (Repeatable)
</order>
```

## Common questions

### Can I generate XML from an XSD automatically?

Yes. XML Lens (New Document from Schema), Visual Studio's XML Schema Explorer, Eclipse, and IntelliJ IDEA can all generate a starting document from a chosen root element. Review the placeholder values and validate the result.

### Which element should be the root?

Any element declared globally, directly under xs:schema. Elements declared inside a complex type are local and cannot be the root.

### Do I need the namespace in my XML?

If the schema has a targetNamespace, yes: the root element, and usually its children when elementFormDefault is qualified, must be in that namespace.

### What does minOccurs="0" mean?

The element is optional. Without minOccurs, an element is required exactly once unless maxOccurs allows more.

### How do I know my XML matches the XSD?

Validate it, for example with xmllint --noout --schema schema.xsd file.xml on a Mac, or in an editor that validates as you type.

## References

- [W3C XML Schema Part 1: Structures (schema location hints)](https://www.w3.org/TR/xmlschema-1/#schema-loc)
- [W3C XML Schema Part 0: Primer](https://www.w3.org/TR/xmlschema-0/)
- [xmllint manual (libxml2)](https://gnome.pages.gitlab.gnome.org/libxml2/xmllint.html)

## Try the workflow in XML Lens

[Download XML Lens for Mac](https://apps.apple.com/us/app/xml-editor-lens-xpath/id6761771384?mt=12&ct=guide-create-xml-from-xsd)

Explore the relevant Mac workspaces and their capabilities.

- [Write valid XML as you type with an XSD on Mac](https://xmllens.ayushdev.com/xml-schema-editor-mac)
- [View and map XML Schema locally on Mac](https://xmllens.ayushdev.com/xsd-viewer-mac)
- [Validate XML and run local XSLT 1.0 on Mac](https://xmllens.ayushdev.com/xml-validation-xslt)

[All XML guides](https://xmllens.ayushdev.com/guides)
