Created
June 18, 2017 15:26
-
-
Save toboid/05ff688375cded9412dccbd0adc12454 to your computer and use it in GitHub Desktop.
Validating xml sitemaps in node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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