DesignLiquido / xslt-processor

A JavaScript XSLT processor without native library dependencies
GNU Lesser General Public License v3.0
102 stars 31 forks source link

How to use in Node.js #22

Closed mig82 closed 5 years ago

mig82 commented 5 years ago

I'm trying to use this library in Node.js to parse an XML file. Here's what I'm doing.

const fs = require('fs-extra');
const xmlParse = require('xslt-processor').xmlParse;
const xml = await fs.readFile('path/to/file.xml');
xmlParse(xml);

I'm getting this error:

(node:14628) UnhandledPromiseRejectionWarning: TypeError: xml.match is not a function
    at xmlParse (/Users/miguelangel/my-node-projects/viskit/node_modules/xslt-processor/dist/xslt-processor.js:490:13)
    at validateProject (/Users/miguelangel/my-node-projects/viskit/controllers/validate-project.js:7:12)
(node:14628) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14628) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I don't think I'm requireing this correctly. How do I make this work in Node?

mig82 commented 5 years ago

Never mind. My bad. I was missing the encoding parameter. From the Node documentation on readFile:

If no encoding is specified, then the raw buffer is returned.

So I guess that attempting to parse the buffer was causing the issue. This fixed it.

const xml = await fs.readFile('path/to/file.xml', 'utf8');