日期:2014-05-18  浏览次数:20732 次

急救:在HTML内嵌JScript代码,如何使用XmlNamespaceManager正确读取有命名空间的XML文件
搞了一下午了,还是没结果。

<script   For= "window "   EVENT= "onload "   language= "javascript ">  
<xsl:comment>  
<![CDATA[  
<script   For= "window "   EVENT= "onload "   language= "javascript ">  
<xsl:comment>  
xmldoc=document.XMLDocument;  
xmldoc.setProperty( "SelectionLanguage ",   "XPath ");  
]]>  
</xsl:comment>  
</script>  

<script   language= "jscript ">  
<xsl:comment   >  
<![CDATA[  
var   xmldoc=null;  
var   node=null;  
var   e=null;  
var   nsmgr=null;

function   edit(id){  

//alert(xmldoc.xml);
nsmgr   =   new   XmlNamespaceManager(xmldoc.NameTable);       //此处在浏览器中老是提示XmlNamespaceManager对象不存在
nsmanager.AddNamespace( "xx ",       "http://tempuri.org/myUSordersnamespace ")      
//XmlNode       book       =       doc.SelectSingleNode( "//ab:book ",       nsmgr);      

node=xmldoc.selectSingleNode( "//results/system[position()= ' "+id+ " '] ",nsmgr);       //此处老是无法取到结果,据网上说是nsMgr的问题
document.all.id.value=id;  
document.all.type.value=node.attributes(0).text;  
document.all.name.value=node.attributes(1).text;  
//document.all.type.value= "node.attributes(0).text ";  
//document.all.name.value= "node.attributes(1).text ";  

e=event.srcElement.parentNode.parentNode;  
}  


------解决方案--------------------
你是在IE下运行?

IE的xml解析器是msxml,没有XmlNamespaceManager

参考这两个:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmpro2nd_selectionnamespaces.asp


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmpro2nd_selectionlanguage.asp
------解决方案--------------------
带名称空间的例子

<script>

str = " <?xml version=\ "1.0\ " encoding=\ "UTF-8\ " ?> <html xmlns=\ "http://www.w3.org/1999/xhtml\ " version=\ "-//W3C//DTD HTML 4.01 Transitional//EN\ "> <body> <div> test </div> </body> </html> "
var doc = new ActiveXObject( "MSxml2.DOMDocument ")
alert(doc.loadXML(str));
doc.setProperty( "SelectionNamespaces ", "xmlns:x= 'http://www.w3.org/1999/xhtml ' ")
var node = doc.selectSingleNode( "//x:div ")
alert(node.text)
</script>