有没有办法将文档节点作为参数传递给Edge和Chrome中的XSLTProcessor?
我正在尝试在边缘进行XSL转换,并使用文档节点作为XSLT参数(通过XSLTProcessor.setParameter)。 transformtoffment在边缘(和铬)中返回零,但在Firefox中运行不错。
有没有办法使下面的代码工作(至少在边缘)或不支持文档节点作为XSLT参数? 以下是提出问题的最小代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script type="text/javascript">
function example() {
let xsldocp = new DOMParser();
const xsldoc = xsldocp.parseFromString(`<?xml version='1.0'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method='xml' omit-xml-declaration='yes'/>
<!-- Parameters -->
<xsl:param name='ntParam'/>
<xsl:param name='strSpaces' select='"    "'/> <!-- Default to 4 spaces -->
<!-- Global variables -->
<xsl:variable name='numTransformLevel' select='$ntParam/PARAMETER/@VALUE'/> <!-- does not work in Edge & Chrome -->
<!--xsl:variable name='numTransformLevel' select='2'/--><!-- works OK -->
<!-- Handles a generic element node. -->
<xsl:template match='*'>
<div>
<b><xsl:value-of select='$numTransformLevel'/></b>
<i><xsl:value-of select='//GROOT/@VALUE'/></i>
</div>
</xsl:template>
</xsl:stylesheet>
`,"application/xml");
let xsltProcessor = new XSLTProcessor();
// use the xsl sheet
xsltProcessor.importStylesheet(xsldoc);
// create xml
let parser = new DOMParser();
const docData = parser.parseFromString("<ROOT><GROOT VALUE='IAM'/></ROOT>", "application/xml");
// create param xml and set the parameter
parser = new DOMParser();
const docParam = parser.parseFromString("<PARAM><PARAMETER VALUE='5'/></PARAM>", "application/xml");
xsltProcessor.setParameter(null, "ntParam", docParam.documentElement);
// do the transformation
var targetElm = document.getElementById("trCont5");
let resultFrag = xsltProcessor.transformToFragment(docData, targetElm.ownerDocument);
targetElm.appendChild( resultFrag );
}
</script>
</head>
<body>
<input type="button" onclick="example();" value="XLSTParam Test"/><!-- Outputs 5IAM -->
<div id="trCont5"></div>
</body>
</html>
I am trying to do XSL transformation in Edge and using a document node as xslt parameter(via XSLTProcessor.setParameter).
The transformToFragment returns null in Edge (and Chrome), but works ok in Firefox.
Is there a way to make the code below working (at least in Edge) or is there no support of document-node as a xslt parameter?
Below is a minimal code to present the issue.
https://jsfiddle.net/BennyHilarious/nxzLf9g3/1/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script type="text/javascript">
function example() {
let xsldocp = new DOMParser();
const xsldoc = xsldocp.parseFromString(`<?xml version='1.0'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method='xml' omit-xml-declaration='yes'/>
<!-- Parameters -->
<xsl:param name='ntParam'/>
<xsl:param name='strSpaces' select='" "'/> <!-- Default to 4 spaces -->
<!-- Global variables -->
<xsl:variable name='numTransformLevel' select='$ntParam/PARAMETER/@VALUE'/> <!-- does not work in Edge & Chrome -->
<!--xsl:variable name='numTransformLevel' select='2'/--><!-- works OK -->
<!-- Handles a generic element node. -->
<xsl:template match='*'>
<div>
<b><xsl:value-of select='$numTransformLevel'/></b>
<i><xsl:value-of select='//GROOT/@VALUE'/></i>
</div>
</xsl:template>
</xsl:stylesheet>
`,"application/xml");
let xsltProcessor = new XSLTProcessor();
// use the xsl sheet
xsltProcessor.importStylesheet(xsldoc);
// create xml
let parser = new DOMParser();
const docData = parser.parseFromString("<ROOT><GROOT VALUE='IAM'/></ROOT>", "application/xml");
// create param xml and set the parameter
parser = new DOMParser();
const docParam = parser.parseFromString("<PARAM><PARAMETER VALUE='5'/></PARAM>", "application/xml");
xsltProcessor.setParameter(null, "ntParam", docParam.documentElement);
// do the transformation
var targetElm = document.getElementById("trCont5");
let resultFrag = xsltProcessor.transformToFragment(docData, targetElm.ownerDocument);
targetElm.appendChild( resultFrag );
}
</script>
</head>
<body>
<input type="button" onclick="example();" value="XLSTParam Test"/><!-- Outputs 5IAM -->
<div id="trCont5"></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用saxonjs( https://www.saxonica.com/saxonica.com/saxonica.com/saxon-js /documentation2/index.html ,在 https://www.saxonica.com /download/javascript.xml ):
请注意,对于示例的紧凑性和独立性,我使用了
saxonjs.xpath.evaluate
方法来调用XPATH 3.1转换
即时运行提供的XSLT。 saxonjs还允许您使用saxonjs.s.transform
方法运行预编译XSLT,因为您需要使用saxonee或node.jsxslt3
tool(来自saxonica/npm https://www.npmjs.coms.com/package/package/xslt3 ) JSON文件。Using SaxonJS (docs at https://www.saxonica.com/saxon-js/documentation2/index.html, download at https://www.saxonica.com/download/javascript.xml):
Note that for compactness and self-containedness of the example I used the
SaxonJS.XPath.evaluate
method to call the XPath 3.1transform
function on the fly to run the provided XSLT. SaxonJS also allows you to run precompiled XSLT with theSaxonJS.transform
method, for that you need to use either SaxonEE or the Node.jsxslt3
tool (from Saxonica/NPM https://www.npmjs.com/package/xslt3) to compile the XSLT file to a JSON file.