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 nodevar xmldom = require"xmldom";var c14n = require"xml-c14n";var xmlData = require"fs"readFileSyncprocessargv2 "utf8"document = parseFromStringxmlData;var canonicaliser = c14ncreateCanonicaliser"";console.log"canonicalising with algorithm: " + canonicalisername;console.log"";console.log"INPUT";console.log"";console.logxmlData;console.log"";canonicalisercanonicalisedocumentdocumentElementif errreturn console.warnerrstack;console.log"RESULT";console.log"";console.logres;;
➜ xml-c14n git:(master) ./example.js small.xmlcanonicalising with algorithm:INPUT<?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 -->RESULT<doc>Hello, world!<!-- Comment 1 --></doc>
Available via npm:
$ npm install xml-c14n
Or via git:
$ git clone git://github.com/deoxxa/xml-c14n.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 = ;// ORvar c14n = CanonicalisationFactory;// THUSvar 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.
This lets you get the factory function for an algorithm. Not incredibly useful, but here for completeness.
var herpDerpFactory = c14ngetAlgorithm"";
Creates an instance of a canonicaliser, referenced by its URI, and optionally passing along something for the new instance.
// creates an xml-exc-c14n canonicaliservar canonicaliser = c14ncreateCanonicaliser"#";
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 = algorithmname;
This is what does the meat of the work in most implementations.
algorithmcanonicalisenodeif errreturn console.warnerr;console.logdata;;
There are two included algorithms, one of which is a specialisation of the other.
For a description of this algorithm and its options, see the xml-exc-c14n specification.
This is just a special version of
3-clause BSD. A copy is included with the source.