使用SaxonJS 2.6加载转换XML/SEF文件时遇错误求助
问题:SaxonJS 2.6 在IIS部署下的加载错误处理
环境与背景
- 工具:SaxonJS 2.6(使用
SaxonJS2.rt.js) - 部署环境:Windows 8/10 + IIS服务器
- 预准备:有效的
.xml数据文件、由XSL文件通过xslt3Node.js模块生成的.sef.json样式文件 - 目标:通过SaxonJS加载
.sef.json并转换展示.xml数据
相关文件代码
index.html
<html> <head> <meta charset="UTF-8" /> <title>SxnTrnsct</title> <script type="text/javascript" src="../saxon-js2.6/SaxonJS2.rt.js"></script> <script type="text/javascript" src="js/sxn_tscrpt.js" ></script> </head> <body> <h2>Smthn ..</h2> <div id="output"></div> </body> </html>
sxn_tscrpt.js
"use strict" var opts = {styleSheetLocation:"../xml/htrnsctstl.sef.json",sourceLocation:"../xml/trnsct.xml"} onload = function() { SaxonJS.transform(opts,"async") }
错误信息
在Opera浏览器访问localhost下的index.html时,控制台抛出以下错误:
SaxonJS2.rt.js:1113 Uncaught (in promise) XError: **No stylesheet supplied** at Object.transform (SaxonJS2.rt.js:1113:178) at onload (sxn_tscrpt.js:9:17) SaxonJS2.rt.js:772 GET http://localhost/mbPrct/xml/trnsct.xml 404 (Not Found)
解决方案
1. 修复文件路径问题(核心原因)
404错误说明浏览器无法找到trnsct.xml,而No stylesheet supplied大概率也是.sef.json路径不正确导致的。需要根据实际部署结构调整相对路径:
假设IIS站点根目录结构如下:
mbPrct/ ├─ index.html ├─ saxon-js2.6/ │ └─ SaxonJS2.rt.js ├─ js/ │ └─ sxn_tscrpt.js └─ xml/ ├─ htrnsctstl.sef.json └─ trnsct.xml
当前代码中的../xml/会跳出站点根目录,需修改为站点根相对路径:
var opts = { styleSheetLocation: "/mbPrct/xml/htrnsctstl.sef.json", sourceLocation: "/mbPrct/xml/trnsct.xml" }
如果index.html直接在站点根目录(即localhost/index.html),则路径改为"/xml/htrnsctstl.sef.json"和"/xml/trnsct.xml"。
2. 配置IIS MIME类型
IIS默认可能不识别.sef.json扩展名,需在IIS管理器中添加对应MIME类型:
- 扩展名:
.sef.json,MIME类型:application/json - 确认
.xml的MIME类型已设置为application/xml或text/xml
3. 增加异步调用错误捕获
SaxonJS.transform的异步调用需处理Promise异常,避免错误被静默吞掉:
onload = async function() { try { await SaxonJS.transform(opts, "async"); } catch (err) { console.error("转换出错:", err); document.getElementById("output").textContent = "加载失败:" + err.message; } }
4. 验证文件权限
确保IIS应用程序池对xml/目录有读取权限,避免因权限问题导致文件无法访问。
内容的提问来源于stack exchange,提问作者mirexS
相关产品推荐
相关产品推荐

