国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > jscript > javascript选取XML节点以及xslt变形-IE篇

javascript选取XML节点以及xslt变形-IE篇

来源:程序员人生   发布时间:2014-04-06 12:26:34 阅读次数:3716次

 建站学院(LieHuo.Net)XML文档 先来看一段代码:

以下为引用的内容:
var doc = null;
var sdoc = null;
try
{
doc = new ActiveXObject("MSXML2.DOMDocument.6.0");
sdoc = new ActiveXObject("MSXML2.DOMDocument.6.0");
}
catch(e)
{
alert(e.toString());
}
if(doc)
document.write("ok");
else
document.write("fail");

doc.async = false;
doc.load("http://localhost/xml/a.xml");
sdoc.async = false;
var r = sdoc.load("http://localhost/xml/a.xsl");
try
{
//var result = doc.transformNode(sdoc.documentElement);

sdoc.setProperty("SelectionNamespaces","xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
var tmp = sdoc.documentElement.selectSingleNode("xsl:template[@mode='b']");
alert(tmp.xml);

var result = doc.transformNode(sdoc);
alert(result);
//document.write(result);
}
catch(e)
{
alert(e.toString());
}


 要在IE中用实现选取节点和进行XSLT变形,要用newActiveXObject("MSXML2.DOMDocument.6.0");生成的对像(后面的版本号6.0根据具体环境会不同)。另外在loadxml文档的时候,如果不异步LOAD的话要把异步属性async设成false,变形还是很简单的,看实上面的代码就能明白了,主要说一下在选取节点的时候,如果有命名空间的话,要调用文档的setProperty"SelectionNamespaces","xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");方法,然后用prefix:nodename的xpath才能用来搜到节点,否则将无法搜到其节点。

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生