node package manager


XML canonicalisation


XML canonicalisation (xml-c14n)

This module performs XML canonicalisation as specified in xml-c14n.

To operate, a preconstructed DOM object is required. Any object that implements the DOM Level 1 API will suffice. I recommend xmldom if you're working with node, or your browser's native DOM implementation if you're not.

This module was originally adapted from some code in xml-crypto by Yaron Naveh, and as such is also covered by any additional conditions in the license of that codebase (which just so happens to be the MIT license).

Look, I know the API feels like Java. It pains me as much to work on it as it will pain you to use it. This whole XML thing is a crock of shit, and is so over-engineered that a factory (yeah, you heard me right) was the best way to implement it. So yeah... Sorry.

I spell canonicalise with an "s". Deal with it.

Also see example.js.

#!/usr/bin/env node
var xmldom = require("xmldom");
var c14n = require("xml-c14n")();
var xmlData = require("fs").readFileSync(process.argv[2], "utf8"),
    document = (new xmldom.DOMParser()).parseFromString(xmlData);
var canonicaliser = c14n.createCanonicaliser("");
console.log("canonicalising with algorithm: " +;
canonicaliser.canonicalise(document.documentElement, function(errres) {
  if (err) {
    return console.warn(err.stack);
➜  xml-c14n git:(master) ./example.js small.xml
canonicalising with algorithm:
<?xml version="1.0"?>
<?xml-stylesheet   href="doc.xsl"
   type="text/xsl"   ?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>Hello, world!<!-- Comment 1 --></doc>
<?pi-without-data     ?>
<!-- Comment 2 -->
<!-- Comment 3 -->
<doc>Hello, world!<!-- Comment 1 --></doc>

Available via npm:

$ npm install xml-c14n

Or via git:

$ git clone git:// node_modules/xml-c14n


This is what you get when you require("xml-c14n"). It's a factory for getting canonicaliser implementation instances.

var c14n = new CanonicalisationFactory();
// OR 
var c14n = CanonicalisationFactory();
// THUS 
var c14n = require("xml-c14n")();


This is how you get a specific canonicalisation algorithm implementation into a factory so that it can be instantiated within and returned to callers. You give it a URI and a factory function (sup dog) and it'll shove it into an object internally so it can be retrieved later on.

c14n.registerAlgorithm(uri, factoryFunction)
c14n.registerAlgorithm("http://herp.derp/", function(options) {
  return new HerpDerp(options);


  • uri - a URI to identify the algorithm
  • factoryFunction - a function that creates instances of the algorithm's implementation


This lets you get the factory function for an algorithm. Not incredibly useful, but here for completeness.

var herpDerpFactory = c14n.getAlgorithm("http://herp.derp/");


  • uri - the URI identifying the algorithm to fetch the factory function for


Creates an instance of a canonicaliser, referenced by its URI, and optionally passing along something for the new instance.

c14n.createCanonicaliser(uri, [options]);
// creates an xml-exc-c14n canonicaliser 
var canonicaliser = c14n.createCanonicaliser("");


This is the abstract "class" that all specific algorithms (should) extend. It provides some stubbed out methods that do nothing useful aside from serving as documentation. These methods should be overridden by specific implementations. What you get back from CanonicalisationFactory.createCanonicaliser will be an extension of this class.

This gives you the name (URI) that the algorithm instance goes by.;
var uri =;


This is what does the meat of the work in most implementations.

algorithm.canonicalise(node, cb);
algorithm.canonicalise(node, function(errdata) {
  if (err) {
    return console.warn(err);

There are two included algorithms, one of which is a specialisation of the other.

  • uri -
  • options
    • includeComments
    • inclusiveNamespaces

For a description of this algorithm and its options, see the xml-exc-c14n specification.

  • uri -
  • options
    • inclusiveNamespaces

This is just a special version of with includeComments enabled.

3-clause BSD. A copy is included with the source.