Open In App

How to Validate XML against XSD in JavaScript ?

XML (Extensible Markup Language) is a widely used format for storing and exchanging structured data. XSD (XML Schema Definition) is a schema language used to define the structure, content, and data types of XML documents. Validating XML against XSD ensures that the XML document conforms to the specified schema.

XML (Extensible Markup Language):

XSD (XML Schema Definition):

Why Validate XML Against XSD?

Validating XML against XSD offers several benefits:

Tools for XML Validation

There are several tools and libraries available for XML validation in JavaScript:

Sample Data Format

data.xml

<book>
<title>The Lord of the Rings</title>
<author>J. R. R. Tolkien</author>
<year>1954</year>
</book>

schema.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="year" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Using dedicated XSD validation library

This approach leverages libraries specifically designed for XSD validation in JavaScript. These libraries handle the complexities of XSD validation, often using external tools or parsers.

Installation: Install the library using the following command.

npm i xsd-schema-validator

Example: Implementation to validate XML against XSD using xsd validation library.

const validator = require('xsd-schema-validator');

const xmlString = require('fs').readFileSync('data.xml', 'utf8');
const xsdPath = 'schema.xsd';

validator.validateXML(xmlString, xsdPath)
  .then(result => {
    if (result.valid) {
      console.log("XML is valid!");
    } else {
      console.error("XML is invalid:", result.errors);
    }
  })
  .catch(error => {
    console.error("Validation error:", error);
  });

Output:

XML is valid!

Using libxmljs for XML Validation

libxmljs is a popular library for XML processing in Node.js. It provides support for XML validation against XSD schemas. Here's how to use libxmljs for XML validation.

Installation :

npm install libxmljs

Example: Implementation to validate XML against XSD using libxmljs library.

const fs = require('fs');
const libxml = require('libxmljs');

// Load XML and XSD files
const xmlString = fs.readFileSync('data.xml', 'utf-8');
const xsdString = fs.readFileSync('schema.xsd', 'utf-8');

// Parse XML and XSD
const xmlDoc = libxml.parseXml(xmlString);
const xsdDoc = libxml.parseXml(xsdString);

// Validate XML against XSD
const isValid = xmlDoc.validate(xsdDoc);

// Check validation result
if (isValid) {
  console.log('XML is valid against XSD.');
} else {
  console.error('XML validation failed:');
  const validationErrors = xmlDoc.validationErrors;
  validationErrors.forEach(
      error => console.error(error.message));
}

Output:

XML is valid against XSD.

Real-world Applications and Use Cases

XML validation against XSD finds applications in various scenarios:

Conclusion

Validating XML against XSD ensures that the XML document follows the specified structure and content rules defined in the schema. While there are various tools and libraries available for XML validation in JavaScript, libxmljs is a powerful choice for Node.js applications, providing support for XML parsing and validation against XSD schemas. By using libxmljs, developers can ensure the integrity and correctness of XML data in their applications.

Article Tags :