Rose
Rose

Reputation: 1498

Convert xml to html using XSLT stylesheet in node.js

Has anyone tried to convert xml file into html webpage using XSLT stylesheet in node.js? My background is in Java. I normally use SAXON to convert XML into HTML webpages. I am a newbie to node.js. I have tried to implement this using few libraries like node_xslt, libxsltjs etc but was not successful. If anyone has tried using other libraries that works with XSLT stylesheet, please post a link. Any help would be appreciated.

Upvotes: 5

Views: 4968

Answers (2)

Clarius
Clarius

Reputation: 1419

At time of writing this works for me...

  1. install saxon...
   > npm install saxon-js (see https://www.npmjs.com/package/saxon-js)
  1. write a little test program
const saxon = require('saxon-js');
const env = saxon.getPlatform(); const doc = env.parseXmlFromString(env.readFile("styles/listview.xsl"));
doc._saxonBaseUri = "dummy"; const sef = saxon.compile(doc);
let xml = "<ROWSET><ROW><EMPLOYEE_ID>107</EMPLOYEE_ID><FIRST_NAME>Summer</FIRST_NAME><LAST_NAME>Payne</LAST_NAME><EMAIL>[email protected]</EMAIL><PHONE>515.123.8181</PHONE><HIRE_DATE>2016-06-07</HIRE_DATE><MANAGER_ID>106</MANAGER_ID><JOB_TITLE>Public Accountant</JOB_TITLE></ROW></ROWSET>";
let html = saxon.transform({
    stylesheetInternal:sef,
    sourceType: "xml",
    sourceText:xml,
    destination: "serialized"}, "async"
    ).then( output => {
      console.log(output.principalResult);
    } );
  1. run the test program from the command line...
   > node test.js

The output should be the transformed XML.

Luck.

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163322

If you want to use Saxon from a Node.js application, you basically have three choices, none of them ideal:

(a) call out to Java, using a variety of mechanisms.

(b) use the port of Saxon/C to Node.js being constructed here: https://github.com/rimmartin/saxon-node This is bleeding-edge stuff and I don't know how far the project has got.

(c) wait for Saxon-JS to arrive any time soon. See http://dev.saxonica.com/blog/mike/2016/02/introducing-saxon-js.html

Upvotes: 3

Related Questions