Skip to content

Instantly share code, notes, and snippets.

@toboid
Created June 18, 2017 15:26
Show Gist options
  • Save toboid/05ff688375cded9412dccbd0adc12454 to your computer and use it in GitHub Desktop.
Save toboid/05ff688375cded9412dccbd0adc12454 to your computer and use it in GitHub Desktop.
Validating xml sitemaps in node.js
const assert = require('assert');
const fs = require('fs');
const libxmljs = require('libxmljs');
// Read the sitemap and schema from the file system
// Could just as easily get these over HTTP
const sitemap = fs.readFileSync('../sitemap.xml');
const schema = fs.readFileSync('./schemas/sitemap.xsd');
// Parse the sitemap and schema
const sitemapDoc = libxmljs.parseXml(sitemap);
const schemaDoc = libxmljs.parseXml(schema);
// Perform validation
const isValid = sitemapDoc.validate(schemaDoc);
// Check results
assert.ok(isValid, sitemapDoc.validationErrors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment