0

I need to transform an XML string using XSLT in Javascript. While the XSLT is stored in its own file, the XML is part of a bigger XML document, and therefor stored as a string in a variable.

My current solution looks as follows:

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);

When I save the content of the variable to a file and include it the way I included the XSLT file, I get the desired output (the transformed XML):

xml = document.implementation.createDocument("","",null);
xml.async = false;
xml.load('xmlinput.xml');

I need a way to include the content of the variable to the xml DOM document...or is there a more elegant way?

thanks in advance

2 Answers 2

2

With Mozilla, Opera, Safari, Chrome and with IE 9 you can do e.g.

var xml = new DOMParser().parseFromString(yourVar, 'application/xml');

to parse the XML markup in the variable yourVar into an XML DOM document.

Sign up to request clarification or add additional context in comments.

Comments

0

You can store XML in a variable and use jQuery as normal to traverse it, i tend to do something like this:

window.auditDoc = $.parseXML("<?xml version='1.0' encoding='UTF-8'?><Bundle><Audit></Audit></Bundle>");

If i'm doing allot of work with a single xml file then i also find it useful to put it up in window.auditDoc and always refer to it as that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.