使用Node.js中的XSLT字符串将XML字符串[不是文件!]转换为HTML [英] Transform XML String [not file!] with XSLT String in Node.js to HTML

查看:92
本文介绍了使用Node.js中的XSLT字符串将XML字符串[不是文件!]转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串。

第一个是xml字符串,另存为字符串,而不是文件中。
第二个是我使用 fs.readFile(...)从XSLT文件加载的字符串。

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(...).

我已经尝试使用 libxslt ,但是由于与MSBuildTools和MSBuildTools有关的某些错误而无法通过 npm 安装它

I already tried using libxslt but can't install it via npm due to some errors about MSBuildTools and so on.

libxslt 是否有其他选择?我已经遇到过 xslt处理器了,但是它只接受文件作为参数。

Are there any alternatives for libxslt? I already came across xslt-processortoo, but it only accepts files as parameters.

编辑1:

为了向您提供概述,可以在此处下载XSLT文件和示例XML文件(均在应用程序中作为字符串处理):

to provide you an overview, the XSLT file and an example XML file (both handled as strings in the app) can be downloaded here:

  • Stylesheet in a ZIP File
  • Example XML Files in a ZIP

推荐答案

我以某种方式解决了我的问题:

I somehow got a workaround for my problem:


  1. 我现在使用 xth 库(通过npm安装: npm i xth

  2. 将其导入到我的函数中: var xth = require('xth');

  3. 将xml字符串放入数据URI: var xml ='data:text / xml'+ encodeURICompon ent(xmlString);

  4. XSLT文件现在不是字符串,而是文件: var xsl ='./../ components / ELGA_Stylesheet_v1-0.xsl';

  5. 然后,我就像在 xth-npm

  1. I now use the xth library (install via npm: npm i xth)
  2. import it into my function: var xth = require('xth');
  3. put the xml String into a data URI: var xml = 'data:text/xml,' + encodeURIComponent(xmlString);
  4. the XSLT file is now not a String but the file: var xsl = './../components/ELGA_Stylesheet_v1-0.xsl';
  5. Then, I just call the method xth as in the example at xth - npm

1 xth( xml,xsl,函数(html){
2 html = html.replace(/& lt // g,<);
3 html = html.replace(/& / g,>);
4 html = html.replace(/& / g,&);
5 openWindow(html);
6});

项目 html 是在使用xslt转换的字符串时,我遇到了最后一个问题:在输出字符串的< script> 标记中,符号<,>和&以&lt ;、&gt;和&amp;造成了问题。第2至4行是此问题的解决方法

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)是我自己的方法,可以在新的电子窗口中打开结果字符串。

openWindow(html)is my own method to open the result string in a new electron window.

注意:一个问题仍然存在:如前所述在此处,通过< a href =#id> 中的#导航无效,因为Chromium不允许导航到数据uri的顶部框架。

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.

这篇关于使用Node.js中的XSLT字符串将XML字符串[不是文件!]转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆