xsl中,xml无法动态添加节点问题
test.xml
-------------------------------------------------
<?xml version= "1.0 "?>
<?xml-stylesheet type= "text/xsl " href= "test.xsl "?>
<cicroxml>
<categoryList>
<category>
<id> 184 </id>
<name> <![CDATA[AAAA]]> </name>
</category>
<category>
<id> 185 </id>
<name> <![CDATA[BBBB]]> </name>
</category>
</categoryList>
<serviceList>
<services>
<parent_id> 200 </parent_id>
<service>
<service_id> <![CDATA[109]]> </service_id>
<name> <![CDATA[11]]> </name>
</service>
</services>
<FAQList/>
</serviceList>
</cicroxml>
test.xsl
----------------------------------
<?xml version= "1.0 " encoding= "UTF-8 "?>
<xsl:stylesheet version= "1.0 " xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:output encoding= "UTF-8 " method= "html "/>
<xsl:template match= "/ ">
<html>
<head>
<script language= "javascript "> <![CDATA[
var docXSL=document.XSLDocument;
var docXML=document.XMLDocument;
function showResponse()
{
var retuenXml = ' <serviceList> <services> <parent_id> 200 </parent_id> <service> <service_id> 109 </service_id> <name> 11 </name> </service> <service> <service_id> 110 </service_id> <name> 22 </name> </service> </services> <FAQList> </FAQList> </serviceList> ';
var rxml = new CreateXMLDOM();
rxml.loadXML(retuenXml);
var newXml = rxml.documentElement;
var serviceList = docXML.selectNodes( "//serviceList ");
serviceList.removeAll();
var cicro = docXML.selectSingleNode( "//cicroxml ");
cicro.appendChild(newXml);//这里就报错了,参数不正确
document.all.mainDisplayArea.innerHTML=docXML.documentElement.transformNode(docXSL);
}
function CreateXMLDOM(){
var ActiveX = new Array( "MSXML2.DOMDocument.5.0 ",
"MSXML2.DOMDocument.4.0 ",
"MSXML2.DOMDocument.3.0 ",
"MSXML2.DOMDocument ",
"Microsoft.XMLDOM ",
"MSXML.DOMDocument ");
for (var i=0;i <ActiveX.length;i++){
try{
return new ActiveXObject(ActiveX[i]);
}
catch(e){}
}
return null;
}
]]> </script>
</head>
<body>
<table border= "0 " cellpadding= "0 " cellspacing= "0 " width= "800 " align= "center ">
<tr height= "22 ">
<xsl:for-each select= "/cicroxml/categoryList/category ">
<TD>
<xsl:value-of select= "name "/>
<button onclick= "showResponse() "> 变变变变 </button>
</TD>
</xsl:for-each>
</tr>
<tr>
<td colspan= "10 ">
<div id= "mainDisplayArea ">
<xsl:apply-templates select= "./* "/>
</div>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match= "/* ">
<xsl:value-of select= "/cicroxml/serviceList/services/service/service_id "/>
</xsl:template>
</xsl:stylesheet>
孟子老大在不?请教个问题,我在xsl中,想把ajax传过来的serviceList节点动态添加到 docXML中 我的操作是先删除原有的serviceList节点,然后用appendChild 方法来将节点加进去。可是报了 参数不正确 这个错误, 这个方法我在html中运行是正确的,但在xsl中怎么就运行不过去,不知道哪的问题,望解答
[解决办法]
帮up
[解决办法]
那就采取方案1,我测试是可以的
[解决办法]
这个问题,其实是因为版本不同,造成实际上的类型不兼容。
版本3无法兼容 3以上版本 的节点,而var docXML=document.XMLDocument;实际上用的是版本3。
执行这段代码就可以知道了:
var xml4 = new ActiveXObject( "MSXML2.DOMDocument.4.0 ");//CreateXMLDOM();
xml4.loadXML( " <root/> ");
var xml3 = new ActiveXObject( "MSXML2.DOMDocument.3.0 ");//CreateXMLDOM();
xml3.loadXML( " <root/> ");
//xml3.documentElement.appendChild(xml4.createElement( "newElem "));//这句提示的是你上面的错误。
xml4.documentElement.appendChild(xml3.createElement( "newElem "));//这句有明显的英文提示告诉你。
解决办法估计只能是,你统一版本3使用。
一般我现在写代码也都用版本3,理由是版本3是现在普及程度最高,且有些API较为方便。