JavaScript XMLDOM XML XSL Internet Explorer - 我无法在浏览器中显示转换后的文件
Posted
技术标签:
【中文标题】JavaScript XMLDOM XML XSL Internet Explorer - 我无法在浏览器中显示转换后的文件【英文标题】:JavaScript XMLDOM XML XSL Internet explorer - I cant display transformed file in the browser 【发布时间】:2020-08-20 10:57:48 【问题描述】:我正在尝试使用 javascript 和带有 MS Internet Explorer 的 XMLDOM 转换 XML 文件,但我无法使其工作。在 IE 上的开发人员工具中没有错误,但没有显示文件。我在 w3c 之类的网站上搜索了几个小时,但我找不到答案。
这是我应该使用的代码:
<html>
<body>
<script type=“text/javascript”>
// Load the XML document
var xml = new ActiveXObject(“Microsoft.XMLDOM”)
xml.async = false
xml.load(“myLibrary.xml”)
// Load the XSL document
var xsl = new ActiveXObject(“Microsoft.XMLDOM”)
xsl.async = false
xsl.load(“libraryStyle_v2.xsl”)
//Do the transformation
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
这是我正在使用的代码:
<!DOCTYPE html>
<html>
<body>
<script type=“text/javascript”>
// Load the XML document
var xml = new ActiveXObject(“Microsoft.XMLDOM”)
xml.async = false
xml.load(“travelDiaries.xml”)
// Load the XSL document
var xsl = new ActiveXObject(“Microsoft.XMLDOM”)
xsl.async = false
xsl.load(“travelDiaries.xsl”)
//Do the transformation
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
我不确定我做错了什么。 我不应该使用与上面不同的代码(除了一些小改动)
这是我的 XML 文件代码:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="travelDiaries.xsl" type="text/xsl"?>
<diaries>
<diary name='Wojciech'>
<entry date='2020/06/12' title='Poland'>
<location>Poland</location>
<description>Trip to see the, family and friends in a home town</description>
<img></img>
</entry>
</diary>
<diary name='Karolina'>
<entry date='2018/04/12' title='Poland'>
<location>Poland</location>
<description>Trip for site visiting, visiting a Capital city of Poland - Warsaw </description>
<img></img>
</entry>
</diary>
<diary name='Kuba'>
<entry date='2019/03/02' title='Czech republic'>
<location>Czech republic</location>
<description>Visiting the Old Praque with friends, seeing most popular sites</description>
<img></img>
</entry>
</diary>
<diary name='Kevin'>
<entry date='2020/11/08' title='Usa'>
<location>Usa</location>
<description>Traveling around different states, meeting people and learning about the culture</description>
<img></img>
</entry>
</diary>
</diaries>
还有我的 XSL 代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/diaries">
<html>
<body>
<table border="5">
<tr bgcolor="lawngreen">
<th>Date</th>
<th>Location</th>
<th>Description</th>
<th>Image</th>
</tr>
<xsl:for-each select="diary/entry">
<xsl:sort select="@date" order="descending"/>
<tr>
<td>
<xsl:value-of select="@date"/>
</td>
<td>
<xsl:value-of select="location"/>
</td>
<td>
<xsl:value-of select="description"/>
</td>
<td>
<img border="1" >
<xsl:attribute name="src">
<xsl:value-of select="img"/>
</xsl:attribute>
</img>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
可能被骗? ***.com/questions/24536618/… 可能的其他骗局? ***.com/questions/41384726/… 【参考方案1】:根据你的xml文件内容,我修改了XML文件和XSLT文件如下:
XML 文件(将扩展名从 .xsl
更改为 .xslt
):
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="travelDiaries.xslt" type="text/xsl"?>
travelDiaries.xslt 文件:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Data collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Date</th>
<th>Location</th>
<th>Description</th>
</tr>
<xsl:for-each select="diaries/diary">
<xsl:sort select="entry/@date" order="descending"/>
<tr>
<td>
<xsl:value-of select="entry/@date"/>
</td>
<td>
<xsl:value-of select="entry/location"/>
</td>
<td>
<xsl:value-of select="entry/description"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
然后,在Html页面中,如果使用IE浏览器,我们可以使用ActiveXObject object and the transform() method to transform the XML document,如果使用Edge或Chrome浏览器,我们可以使用XSLTProcessor object来转换XML文档(查看XSLTProcessor Basic Example)。请检查以下示例代码:
HTML页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="example">
</div>
<script>
var xml;
var xsl;
function loadXMLDoc(filename)
if (window.ActiveXObject || "ActiveXObject" in window)
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
else
xhttp = new XMLHttpRequest();
xhttp.open("GET", filename, false);
xhttp.send("");
return xhttp.responseXML;
if (window.ActiveXObject || "ActiveXObject" in window)
ie();
else
xml = loadXMLDoc("XMLPage.xml");
xsl = loadXMLDoc("travelDiaries.xslt");
if (typeof XSLTProcessor !== 'undefined')
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToDocument(xml, document);
var serializer = new XMLSerializer();
var transformed = serializer.serializeToString(resultDocument.documentElement);
//console.log(transformed);
document.getElementById("example").innerHTML = transformed;
// https://msdn.microsoft.com/en-us/library/ms753809(v=vs.85).aspx
function ie()
var xslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
var xslProc;
xslDoc.async = false;
xslDoc.load("travelDiaries.xslt");
if (xslDoc.parseError.errorCode != 0)
var myErr = xslDoc.parseError;
alert("You have error " + myErr.reason);
else
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.load("XMLPage.xml");
if (xmlDoc.parseError.errorCode != 0)
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
else
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.addParameter("param1", "Hello");
xslProc.transform();
//console.log(result);
var result = xslProc.output;
if (result !== undefined)
document.getElementById("example").innerHTML = result;
</script>
</body>
</html>
IE 11 输出:
【讨论】:
我不明白为什么要使用XSLTProcessor
的代码检查if (document.implementation && document.implementation.createDocument)
而不仅仅是if (typeof XSLTProcessor !== 'undefined')
。如果您想使用某个对象(构造函数),例如 XSLTProcessor
,为什么还要检查不相关的属性,例如 document.implementation
和/或 document.implementation.createDocument
?
@MartinHonnenma,感谢您提醒我,它更简单明了..^_^以上是关于JavaScript XMLDOM XML XSL Internet Explorer - 我无法在浏览器中显示转换后的文件的主要内容,如果未能解决你的问题,请参考以下文章