Why not working canonicalizer XML in nojejs xml-c14n

398 Views Asked by At

I have problem in XML, when I need that a element child contain Parent's attributes in Nodejs. Now only atribute show in child canonicalized is xmlns, but, for instance, there are attributes xmlns:ns1, xmlns:types, the attributes don't in child element after function canonicalize.

I cant't to search function/lib in Node, make this for me.

In PHP I can do this, with function C14N.

In PHP i have :

Input :

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<ns1:SendTest xmlns:ns1="http://localhost:8080/TestA/a" xmlns:type="http://localhost:8080/TestA/b" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost:8080/TestA/a http://localhost:8080/TestA/xsd/SendTest.xsd">
   <HEAD>
      <TESTA>6291</TESTA>
   </HEAD>
   <TESTB Id="testeb:1ABCDZ">
      <TESTC>
         <TESTD>e4c47c91392cd57088c28468be0ef2349782f2df</TESTD>
      </TESTC>
   </TESTB>
</ns1:SendTest>';

PHP Code:

$d = new DOMDocument('1.0');
$d->loadXML($xml);    
$data = $d->getElementsByTagName('TESTB')->item(0)->C14N(FALSE,FALSE,NULL,NULL);
echo ($data);
die();

OUTPUT :

<?xml version="1.0" encoding="UTF-8"?>
<TESTB xmlns:ns1="http://localhost:8080/TestA/a" xmlns:type="http://localhost:8080/TestA/b" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="testeb:1ABCDZ">
   <TESTC>
      <TESTD>e4c47c91392cd57088c28468be0ef2349782f2df</TESTD>
   </TESTC>
</TESTB>

not Working in NodeJs (xml-c14n) :

const c14n      = require("xml-c14n")();
const DOMParser = require("xmldom").DOMParser;
async function test (){
  let xml = xml(input PHP);
  let doc = (new DOMParser()).parseFromString(xml);
  let canonicalizer = c14n.createCanonicaliser("http://www.w3.org/2001/10/xml-exc-c14n#");
  let lote = doc.getElementsByTagName('TESTB');
  let canonicalized = await canonicalizer.canonicalise(lote[0]);
  console.log(canonicalized.toString());
}
test();

output :

<TESTB Id="testeb:1ABCDZ">
            <TESTC>
               <TESTD>e4c47c91392cd57088c28468be0ef2349782f2df</TESTD>
            </TESTC>
          </TESTB>

The result I get is not correct,suggestion for my question?

0

There are 0 best solutions below