I have two strings.
The first one is a xml string, saved as a string, not from a file. The second one is a string I load from a XSLT file with fs.readFile(...)
.
I already tried using libxslt
but can't install it via npm
due to some errors about MSBuildTools and so on.
Are there any alternatives for libxslt
? I already came across xslt-processor
too, but it only accepts files as parameters.
EDIT 1:
to provide you an overview, the XSLT file and an example XML file (both handled as strings in the app) can be downloaded here:
I somehow got a workaround for my problem:
xth
library (install via npm: npm i xth
)var xth = require('xth');
var xml = 'data:text/xml,' + encodeURIComponent(xmlString);
var xsl = './../components/ELGA_Stylesheet_v1-0.xsl';
Then, I just call the method xth as in the example at xth - npm
1 xth(xml, xsl, function (html) { 2 html = html.replace(/</g, "<"); 3 html = html.replace(/>/g, ">"); 4 html = html.replace(/&/g, "&"); 5 openWindow(html); 6 });
the item html
is the xslt transformed string, I had one final problem: in the <script>
Tags of the output string, the symbols <,> and & were there as &lt;, &gt;, and &amp; which caused problems. lines 2 to 4 are the workaround for this issue
openWindow(html)
is my own method to open the result string in a new electron window.
NOTE: one problem remains: as mentioned here , navigation via # in <a href=#id>
doesn't work, because Chromium doesn't allow navigation to top frame to data uri.
Disclaimer: not tested for the attached XML/XSLT.
You could use saxon-js
:
const saxon = require("saxon-js");
const env = saxon.getPlatform();
const doc = env.parseXmlFromString(yourStringXSLT);
// monkey-patch to avoid a "Required cardinality of value of parameter $static-base-uri is exactly one; supplied value is empty" compile error
doc._saxonBaseUri = "file:///";
const sef = saxon.compile(doc);
// you can save and reuse the SEF for future transforms, if applicable
const resultStringXML = saxon.transform({
stylesheetInternal: sef,
sourceText: yourStringXML
});
This approach may not be perfect (esp. the _saxonBaseUri
hack) - but I couldn't find anything better (for saxon-js
) documented online (except for the xslt3
command-line based approach).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.