XML Validator

Ensure your XML documents are well-formed and valid

✅ What is XML Validation?

XML validation checks if your document follows proper syntax rules and conforms to a defined structure. Well-formed XML follows syntax rules, while valid XML also matches a DTD or Schema definition.


<!-- Well-formed XML -->
<?xml version="1.0"?>
<note>
    <message>This is valid!</message>
</note>
                                    

Key Validation Concepts

📝

Well-Formed

Follows XML syntax rules

<tag>content</tag>

Valid

Matches DTD or Schema

<!DOCTYPE note SYSTEM>
🔍

Validators

Tools to check XML

xmllint --valid file.xml
⚠️

Errors

Identify and fix issues

Error: Tag mismatch

🔹 Well-Formed XML Rules

Well-formed XML must follow strict syntax rules. Every opening tag needs a closing tag, elements must be properly nested, attribute values must be quoted, and there must be exactly one root element. These rules ensure XML can be parsed correctly.

<!-- ✓ CORRECT: Well-formed XML -->
<?xml version="1.0" encoding="UTF-8"?>
<library>
    <book id="1">
        <title>XML Basics</title>
        <author>John Doe</author>
    </book>
</library>

<!-- ✗ WRONG: Missing closing tag -->
<book>
    <title>XML Basics
</book>

<!-- ✗ WRONG: Improper nesting -->
<book><title>XML Basics</book></title>

<!-- ✗ WRONG: Unquoted attribute -->
<book id=1>Title</book>

Well-Formed Rules:

✓ All tags must close

✓ Proper nesting required

✓ One root element

✓ Attributes in quotes

✓ Case-sensitive tags

🔹 Valid XML

Valid XML goes beyond being well-formed by conforming to a specific structure defined in a DTD or XML Schema. Validation ensures your document contains the correct elements, attributes, and data types in the proper order and hierarchy.

DTD Definition:

<!DOCTYPE note [
    <!ELEMENT note (to, from, message)>
    <!ELEMENT to (#PCDATA)>
    <!ELEMENT from (#PCDATA)>
    <!ELEMENT message (#PCDATA)>
]>

Valid XML Document:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
    <to>Alice</to>
    <from>Bob</from>
    <message>Hello!</message>
</note>

Validation Result:

✓ Document is valid

All required elements present

Correct element order

🔹 Common Validation Errors

Understanding common XML errors helps you write better documents and debug issues faster. These errors range from simple syntax mistakes to structural problems that violate DTD or Schema rules.

<!-- Error 1: Unclosed tag -->
<book>
    <title>XML Guide
</book>
<!-- Error: Element 'title' not closed -->

<!-- Error 2: Case mismatch -->
<Book>
    <title>XML Guide</title>
</book>
<!-- Error: Tag 'Book' doesn't match 'book' -->

<!-- Error 3: Overlapping tags -->
<book><title>XML</book></title>
<!-- Error: Improper nesting -->

<!-- Error 4: Missing quotes -->
<book id=123>Title</book>
<!-- Error: Attribute value not quoted -->

<!-- Error 5: Invalid characters -->
<price>$29.99 & up</price>
<!-- Error: Use &amp; for & character -->

🔹 Using XML Validators

XML validators are tools that automatically check your documents for errors. They can be online services, command-line tools, or built into code editors. Validators provide detailed error messages to help you fix issues quickly.

Popular Validation Tools:

  • Online Validators: W3C Validator, XMLValidation.com
  • Command Line: xmllint (Linux/Mac), msxsl (Windows)
  • Code Editors: VS Code, Notepad++, Oxygen XML
  • Programming: DOM Parser, SAX Parser, XML libraries
# Command line validation
xmllint --noout --valid document.xml

# With DTD
xmllint --noout --dtdvalid note.dtd document.xml

# With Schema
xmllint --noout --schema note.xsd document.xml

🔹 Validation in JavaScript

You can validate XML programmatically using JavaScript's DOMParser. This is useful for web applications that need to check XML data before processing it. The parser will throw errors for malformed XML.

// Validate XML in JavaScript
function validateXML(xmlString) {
    const parser = new DOMParser();
    const xmlDoc = parser.parseFromString(xmlString, "text/xml");
    
    // Check for parsing errors
    const errors = xmlDoc.getElementsByTagName("parsererror");
    
    if (errors.length > 0) {
        console.log("Invalid XML:", errors[0].textContent);
        return false;
    } else {
        console.log("Valid XML!");
        return true;
    }
}

// Test
const xml = '<?xml version="1.0"?><note><msg>Hello</msg></note>';
validateXML(xml);

🧠 Test Your Knowledge

What is the difference between well-formed and valid XML?