addParameter javascript(用于xslt)函数不起作用 [英] addParameter javascript (used for xslt) function doesn't work
问题描述
我正在尝试使用javascript调用的 xslt
查询 xml
文件。
I'm trying to query an xml
file using xslt
called by javascript.
以下是主页:
<html>
<head>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
function displayResult()
{
xml=loadXMLDoc("FooBar.xml");
xsl=loadXMLDoc("FooBar.xsl");
// code for IE
if (window.ActiveXObject)
{
ex=xml.transformNode(xsl);
document.getElementById("Main").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.addParameter("numFoobar","2");
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("Main").appendChild(resultDocument);
}
}
</script>
</head>
<body onload="displayResult()">
<div id="Main">
</div>
</body>
</html>
xml
文件很简单:
<?xml version="1.0" standalone="yes"?>
<Foobars xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Foobar>
<numFoobar>1</numFoobar>
<nameFoobar>Foo</numFoobar>
</Foobar>
<Foobar>
<numFoobar>2r</numFoobar>
<nameFoobar>Bar</nameFoobar>
</Foobar>
</Foobars>
和 xslt
:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Num</th>
<th>Name</th>
</tr>
<xsl:param name="numFoobar"
<xsl:for-each select="Foobars/Foobar">
<tr>
<td><xsl:value-of select="numFoobar"/></td>
<td><xsl:value-of select="nameFoobar"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如您所见,在 xslt
文件我添加了参数。我在javascript中使用它来过滤 numFoobar
。
As you can see, in the xslt
file I added a parametere. I use it in the javascript to filter by numFoobar
.
问题是Safari返回错误:
The problem is that the Safari returns that error:
TypeError: Result of expression 'xsltProcessor.addParameter' [undefined] is not a function.
那么,为什么 addParameter
无法识别?
So, why the addParameter
is not recognized?
谢谢,
问候。
推荐答案
来自 https://developer.mozilla.org / en / The_XSLT / JavaScript_Interface_in_Gecko:Setting_Parameters
XSLT提供xsl:param元素,
是一个孩子xsl:stylesheet
元素。XSLTProcessor()
提供
三种JavaScript方法来与这些参数进行交互
:setParameter
,
getParameter
和removeParameter
。它们
全部作为xsl:param
的
命名空间URI的第一个参数(通常该参数将落在
默认命名空间中,因此传入
null就足够了。xsl:param的本地名称
是第二个
参数。 setParameter需要一个
的第三个参数 - 即参数将被设置为
的值。
XSLT provides the xsl:param element, which is a child of the xsl:stylesheet element.
XSLTProcessor()
provides three JavaScript methods to interact with these parameters:setParameter
,getParameter
andremoveParameter
. They all take as the first argument the namespace URI of the xsl:param (Usually the param will fall in the default namespace, so passing in "null" will suffice.) The local name of the xsl:param is the second argument. setParameter requires a third argument - namely the value to which the parameter will be set.
我不能找不到Opera(Presto),Safari和Chrome(Webkit)的文档。随意编辑这个答案。
I couldn't find documentation for Opera (Presto), or Safari and Chrome (Webkit). Feel free to edit this answer.
这篇关于addParameter javascript(用于xslt)函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!