Anybody worked on XML to HTML transform in Nodejs, Iam getting "has no method 'apply'"error
var fs = require('fs');
var libxslt = require('libxslt');
var libxmljs = require('libxmljs');
var docSource = fs.readFileSync('Hello.xml', 'utf8');
var stylesheetSource = fs.readFileSync('Hello.xsl', 'utf8');
var stylesheet = libxmljs.parseXml(stylesheetSource);
var result = stylesheet.apply(docSource);
res.end(result);
Error:
TypeError: Object <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/hello-world"><HTML><HEAD><TITLE/></HEAD><BODY><H1><xsl:value-of select="greeting"/></H1>hello</BODY></HTML> </xsl:template>
</xsl:stylesheet>
has no method 'apply'
at C:\WEBROOT\DM\wwwRoot\hello\node_modules\Simple_Server.js:151:42
at Layer.handle [as handle_request]
The error you means your
stylesheet.apply(docSource)failed becausestylesheetdoesn't have that method. Looking atthe docs for xsltmakes it pretty clear that.applyis on the results oflibxslt.parse, not the result oflibxmljs.parseXml. So you need to do: