Last active
August 29, 2015 14:21
-
-
Save bjrmatos/dae217da9ea5e1e3a492 to your computer and use it in GitHub Desktop.
Testing output of xmldom serializer (.toString) with xml namespaces
This file contains 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
'use strict'; | |
var dom = require('xmldom').DOMParser, | |
select = require('xml-crypto').xpath, | |
c14n = require("xml-c14n")(); | |
var canonicaliser = c14n.createCanonicaliser("http://www.w3.org/2001/10/xml-exc-c14n#"); | |
var doc = new dom().parseFromString( | |
'<root xmlns:ds="http://www.w3.org/2000/09/xmldsig#">' + | |
'<name>Boris</name>' + | |
'<ds:Signature>Hola</ds:Signature>'+ | |
// si uso este el Signature de abajo, el namespaceURI es | |
// incluido correctamente al hacer signatureNode.toString() | |
// '<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">Hola</ds:Signature>'+ | |
'</root>' | |
); | |
var signatureNode = select(doc, "/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']")[0] | |
console.log('Signature Node:'); | |
// debido a que esto no produce <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">Hola</ds:Signature> | |
// el namespaceURI generado del nuevo documento es incorrecto (queda como vacio) | |
console.log(signatureNode.toString()); | |
var doc2 = new dom().parseFromString(signatureNode.toString()); | |
console.log('Signature Node (after parsing again):'); | |
console.log(doc2.documentElement.toString()); | |
/* USANDO C14N PRODUCE EL OUTPUT DESEADO */ | |
canonicaliser.canonicalise(signatureNode, function(err, res) { | |
if (err) { | |
return console.error(err); | |
} | |
console.log('Signature Node (after parsing with c14n):'); | |
console.log(res); | |
}); |
This file contains 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
{ | |
"name": "test-xml-crypto", | |
"version": "1.0.0", | |
"description": "Test xml-crypto", | |
"main": "app.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "BJR Matos <bjrmatos@gmail.com>", | |
"license": "MIT", | |
"dependencies": { | |
"xml-c14n": "0.0.6", | |
"xmldom": "^0.1.19" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment