Two Levels of XML Validation
Well-formed means the markup rules are satisfied: tags nest correctly, attributes are quoted, entities are escaped. Valid against a schema means the document also matches an XSD (or DTD/RelaxNG) contract—required elements, types, and structure.
Partner systems often reject files that are well-formed but not schema-valid. Check both.
| Level | Catches |
|---|---|
| Well-formed | Mismatched tags, unescaped &, bad quotes, illegal characters |
| Schema (XSD) | Missing required fields, wrong types, unexpected elements |
| Business rules | Duplicate IDs, cross-field constraints beyond XSD |
Step-by-Step: Well-Formed Check
Any XML parser that throws on the first error is enough for this level.
CLI with xmllint
Fast feedback in CI and on a laptop.
xmllint --noout orders.xml && echo "well-formed"Python
ElementTree raises ParseError on broken markup.
import xml.etree.ElementTree as ET
try:
ET.parse("orders.xml")
print("well-formed")
except ET.ParseError as e:
print("not well-formed:", e)Node.js (fast-xml-parser)
Validate before you trust the object tree.
import { XMLValidator } from "fast-xml-parser";
import fs from "node:fs";
const xml = fs.readFileSync("orders.xml", "utf8");
const result = XMLValidator.validate(xml);
if (result !== true) {
console.error(result);
}Step-by-Step: XSD Schema Validation
When a partner sends an .xsd file, validate against it before every upload.
xmllint --noout --schema orders.xsd orders.xmlWhat XSD typically enforces
Element order and cardinality, string patterns, numeric ranges, required attributes, and allowed child elements. It does not replace all business logic, but it catches most structural mistakes early.
Common Well-Formed Failures
These errors show up constantly in hand-built or CSV-generated XML.
- Unescaped & in company names and URLs.
- Mismatched or overlapping tags.
- Attribute values without quotes.
- Invalid characters in element names (spaces, leading digits).
- Multiple top-level elements without a single root.
<!-- Bad -->
<name>Ada & Charles</name>
<!-- Good -->
<name>Ada & Charles</name>Common Schema Failures
The document parses, but the partner portal still rejects it.
- Missing required elements or attributes.
- Wrong namespace on the root element.
- Unexpected extra fields not in the XSD.
- Values that fail xs:date, xs:decimal, or pattern facets.
- Wrong repeating element cardinality (minOccurs/maxOccurs).
Validate After CSV → XML
Generated XML from spreadsheets is a high-risk path. Clean headers in the Online CSV Editor, convert with CSV to XML, then run xmllint --schema before SFTP upload. If validation fails, fix the CSV columns—not the XML by hand—so the next run stays clean.
Real-World Examples
Where validation pays for itself.
CI gate for partner feeds
A pipeline fails the build if xmllint --schema exits non-zero, so bad XML never reaches SFTP.
Manual ops upload
Before a government portal submission, ops validates locally and fixes encoding and required fields.
Inbound partner XML
An API rejects schema-invalid payloads with field-level errors instead of writing partial database rows.
Common Mistakes
Skipping validation is the most expensive shortcut.
- Only checking well-formedness when an XSD exists.
- Editing XML in Excel and re-saving as something that is no longer XML.
- Ignoring namespace URIs in the schema.
- Validating once manually and never again after the CSV changes.
- Treating "opened fine in a browser" as schema validation.
Best Practices
Make validation boring and automatic.
- Keep the XSD in version control next to feed generators.
- Validate in CI and before every production upload.
- Prefer generating XML from clean CSV over hand-editing markup.
- Log xmllint errors with line numbers for ops.
- After fixing, re-flatten with XML to CSV to confirm business values.
Why Use Convert CSV Online?
Convert CSV Online is free, browser-based, and requires no account for everyday conversions. Build XML from cleaned CSV, or flatten partner XML to CSV for human review after a schema failure. Client-side workflows work on Windows, macOS, and Linux browsers.
Fix data, then re-validate
Most schema errors are bad source values. Fix them in a spreadsheet view, regenerate XML, and validate again.
Conclusion
Well-formed checks catch broken markup. XSD validation catches contract mismatches. Run both before partner systems reject your file—or worse, accept a partial import.
FAQ
How do I validate XML?
First ensure it is well-formed with a parser (xmllint --noout or ElementTree). Then validate against the partner XSD with xmllint --schema if a schema exists.
What is the difference between well-formed and valid XML?
Well-formed means the XML syntax rules are satisfied. Valid means the document also conforms to a schema such as XSD.
What tool should I use to validate XML against XSD?
xmllint (libxml2) is the common CLI choice. IDEs and language libraries also support XSD validation.
Why does my XML fail partner validation?
Usually a missing required field, wrong namespace, type mismatch, or unexpected element—not a missing closing tag.
Can I validate XML generated from CSV?
Yes—and you should. Convert CSV to XML, then run schema validation before upload.
Does a browser “XML view” validate my schema?
No. Browsers may show a tree for well-formed XML; they do not apply your partner’s XSD.
References
Convert your CSV in the browser
Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.