使用存储在变量中的XML进行Javascript XSLT转换
我需要在Javascript中使用XSLT转换XML字符串。虽然XSLT存储在自己的文件中,但XML是更大的XML文档的一部分,因此作为字符串存储在变量中 我当前的解决方案如下所示:使用存储在变量中的XML进行Javascript XSLT转换,javascript,jquery,xml,xslt,Javascript,Jquery,Xml,Xslt,我需要在Javascript中使用XSLT转换XML字符串。虽然XSLT存储在自己的文件中,但XML是更大的XML文档的一部分,因此作为字符串存储在变量中 我当前的解决方案如下所示: xslt = document.implementation.createDocument("","",null); xslt.async = false; xslt.load('xslfile.xsl'); xml = document.implementation.createDocument("","",n
xslt = document.implementation.createDocument("","",null);
xslt.async = false;
xslt.load('xslfile.xsl');
xml = document.implementation.createDocument("","",null);
// here I need to include the XML as it is in the document
xsltProc.importStylesheet(xslt);
xml_dom = xsltProc.transformToDocument(xml);
output += new XMLSerializer().serializeToString(xml_dom.documentElement);
当我将变量的内容保存到一个文件并以包含XSLT文件的方式包含它时,我得到了所需的输出(转换的XML):
我需要一种将变量的内容包含到XMLDOM文档中的方法……还是有一种更优雅的方法
提前感谢使用Mozilla、Opera、Safari、Chrome和IE 9,您可以进行以下操作:
var xml = new DOMParser().parseFromString(yourVar, 'application/xml');
要将变量
yourVar
中的XML标记解析为XML DOM文档。您可以将XML存储在变量中,并使用jQuery正常遍历它,我倾向于这样做:
window.auditDoc = $.parseXML("<?xml version='1.0' encoding='UTF-8'?><Bundle><Audit></Audit></Bundle>");
window.auditDoc=$.parseXML(“”);
如果我使用一个xml文件分配工作,那么我发现将它放在window.auditDoc中并始终引用它也是很有用的
window.auditDoc = $.parseXML("<?xml version='1.0' encoding='UTF-8'?><Bundle><Audit></Audit></Bundle>");