Xsl transformToDocument在chrome浏览器中不起作用,但在firefox中工作

时间:2016-09-10 06:39:50

标签: javascript java xml xslt dom

我通过调用一个URL获取xsl并解析我在代码片段下使用的xml。

var xslt = getXSLTemplate(xslUri, params, xmlDoc);
    if (!xslt) {
        xslt = createXSLTemplateGeneric(xslUri, params, xmlDoc);
        if (xslt) {
            putXSLTemplate(xslUri, xslt);
        }
    }
    if (xslt) {
        xslt.clearParameters();
        var paramName = params[0];
        var paramValue = params[1];
        var result = "";
        for (var paramIdx = 0; paramIdx < paramName.length; paramIdx++) {
            xslt
                    .setParameter("", paramName[paramIdx],
                            paramValue[paramIdx]);
        }
        var transformDoc = xslt.transformToDocument(xmlDoc);
        var xmlSerializer = new XMLSerializer();

        if (transformDoc.firstChild != null) {
            var nodes = transformDoc.lastChild.childNodes;
            for (var i = 0; nodes[i]; ++i) {
                result += xmlSerializer.serializeToString(nodes[i]);
            }
            result = transformDoc.lastChild.xml;
        }
        return result;
    }

但是这段代码在firefox中工作,同样在chrome中不起作用。 请告诉我解析文档的错误是什么,或者如果上述说明不足以进行分析,请告诉我。

我在chrome中得到的结果如下

<div xmlns="http://www.w3.org/1999/xhtml" id="alertMessages" style="width:400px;"></div>

在Firefox中我得到了正确的结果。

<table xmlns=\"http://www.w3.org/1999/xhtml\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"panelContentTbl\" style=\"margin-left:14px; margin-top:14px;\"><tbody><tr><td valign=\"top\"><script id=\"doLogin\" cancelifviewbusy=\"\">try{ if(getFormInputByName(\"loginButton\").disabled == false){ var name = trimAll(getFormInputByName(\"username\").value); var pwd = trimAll(getFormInputByName(\"password\").value); if(name.length &gt; 0 &amp;&amp; pwd.length &gt; 0){   disableButton(\"loginButton\"); displayClientStatus(\"Authenticating user...\");  var nameArray = new Array(); var typeArray = new Array(); var valueArray = new Array();   nameArray.push(\"username\"); typeArray.push(TYPE_ATTRIBUTE); valueArray.push(name);   nameArray.push(\"password\"); typeArray.push(TYPE_ATTRIBUTE); valueArray.push(pwd);   loadAsyncPanelAction(\"loginWidget\", getActionUri('client', 'login'), nameArray, typeArray, valueArray, \"checkSecurityAuth(asyncPanelRequest);\");  } else{ panelAlert(\"Please enter your user ID and password.\",\"layouts/layout2010.1/xsl/widget/panel/login.xsl\");} } } catch(anErr){ errorAlert(\"doLogin\", SEVERITY_HIGH, anErr); } </script><script id=\"getPasswordHelp\" cancelifviewbusy=\"\">try{ var name = trimAll(getFormInputByName(\"username\").value); var nameArray = new Array(); var typeArray = new Array(); var valueArray = new Array(); nameArray.push(\"username\"); typeArray.push(TYPE_ATTRIBUTE); valueArray.push(name); displayClientStatus(\"Loading password assistant...\");loadAsyncPanelAction(\"loginWidget\", getActionUri('layout.widget', 'get-password-help'), nameArray, typeArray, valueArray, \"displayClientStatus('');\"); } catch(anErr){ errorAlert(\"getPasswordHelp\", SEVERITY_HIGH, anErr); } </script><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody><tr><td><div id=\"panelHeader\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody><tr class=\"panelHeader\"><td nowrap=\"yes\">User Login</td></tr></tbody></table></div></td></tr><tr><td valign=\"top\" style=\"padding:10px;\"><div id=\"panel\" style=\"330px\"><div style=\"white-space:normal;\"><instructions><span>Please enter your user ID and password.</span></instructions></div><div class=\"inputHeader\"></div><eventhandler id=\"initContent\" handler=\"var fi = getFormInputByName('username'); if (null != fi) fi.focus();\"> </eventhandler><table cellspacing=\"1\" cellpadding=\"0\" border=\"0\" class=\"panelForm\" id=\"loginForm\"><tbody><tr><td id=\"panelLabel\" style=\"width:120px;\">User ID *</td><td nowrap=\"true\" id=\"panelValue\" style=\"width:120;\"><input type=\"text\" id=\"inputProperty\" class=\"inputText\" name=\"username\" size=\"30\" required=\"FALSE\" onselectstart=\"setSelectable(this,true,event);\" ondragenter=\"event.returnValue = false;\" value=\"\" style=\"-moz-user-select:text;width:175pxpx;\" onkeypress=\"clickButtonOnEnter(event, 'loginButton');\" /></td></tr><tr><td id=\"panelLabel\" style=\"width:120px;\">Password *</td><td nowrap=\"true\" id=\"panelValue\" style=\"width:120;\"><input type=\"password\" class=\"inputText\" name=\"password-display\" size=\"30\" value=\"\" onselectstart=\"setSelectable(this,true,event);\" ondragenter=\"event.returnValue = false;\" style=\"-moz-user-select:text;width:175pxpx;\" onchange=\"getFormInputByName('password').value = this.value;\" onkeypress=\"clickButtonOnEnter(event, 'loginButton');\" /><input type=\"hidden\" id=\"inputProperty\" name=\"password\" value=\"\" /><eventhandler id=\"resizeContent\" handler=\"var pwd1 = getFormInputByName('password-display'); var pwd2 = getFormInputByName('password'); if (null != pwd1 &amp;&amp; null != pwd2) pwd1.value = pwd2.value;\"> </eventhandler></td></tr><tr><td id=\"panelLabel\"> </td><td id=\"panelValue\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody><tr><td nowrap=\"nowrap\" style=\"padding-right:6px;\"><input type=\"submit\" id=\"button\" name=\"loginButton\" value=\"Login\" title=\"\" style=\"width:70px;\" class=\"panelButton\" onclick=\"\n                            if(jQuery(this).hasClass('buttonDisabled') ||\n                            jQuery(this).hasClass('jQueryDisabled')) return;\n                            fireScript('doLogin'); \" /></td></tr></tbody></table></td></tr></tbody></table><div align=\"right\"><table><tbody><tr><td align=\"right\"><a class=\"blue\" style=\"padding-top:8px; cursor:pointer; white-space:nowrap;\" onclick=\"fireScript('getPasswordHelp');\">Forgot your password?</a></td></tr></tbody></table></div></div></td></tr><tr><td valign=\"top\" height=\"100%\" style=\"text-align:center;\" id=\"clientStatus\"></td></tr></tbody></table></td></tr></tbody></table>

任何浏览器都没有崩溃或错误。

1 个答案:

答案 0 :(得分:1)

这可能取决于您如何实际获得文档xmlDoc。如果使用ajax DO 检索文档,则返回xhttp.responseXML,而请勿返回xhttp.responseXML.documentElement

两者都可以使用XMLSerializer转换为有意义的文档表示形式,但是只有第一条语句是XSL转换的可行参数。

如果使用后者,则在我的情况下(使用Chrome),XSLTransformation的返回值(使用toDocument或toFragment函数)为空