XSL question: What's the difference between a template processor vs. DOMDocument?

In my application, I'm currently using DOMDocument40 to load my XML and XSL files.

1. What is a template processor and when should it be used?
2. Using JavaScript, how is code written to use the template processor?

Thanks.

halfondjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rdcproCommented:
There are two main differences:

1.  template processor outputs to a stream, which maintains encoding among other things.  This is much more important when done server-side.  However, xml.transformNodeToObject(xsl) also transforms to a stream
2.  template processor allows you to set parameters in the XSLT.

Other features:

*  it allows caching of XSLT, which may be important
*  can be a bit faster

Here's the basic code:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.load("myXMLData.xml");

var xmlResults = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlResults.async = false;
xmlResults.load("myXMLData.xml");


// Must be free threaded for XSLTemplate processor
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
xslDoc.async = false;
xslDoc.load("myStylesheet.xslt");

var myTemplate = Server.CreateObject("Msxml2.XslTemplate.4.0");
myTemplate.stylesheet = xslDoc;
 
var myProc = myTemplate.createProcessor();
myProc.input = xmlDoc;

 //write response to client using IStream
myProc.output = Response;
//write result to XML document.  Make sure XSLT uses <xsl:output method="xml"/>
myProc.output = xmlResults;

// myStream can be any object that supports IStream, such as an ADO memory stream
myProc.output = myStream;

// Optionally set parameters here
myProc.setParameter("ParameterName", "value");

myProc.transform();

// client side code on a web page, you would now write the output to a div:
myDiv.innerHTML = xmlResults.xml;

Regards,
Mike Sharp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rdcproCommented:
made a boo boo in the code above:

var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");

should be:

var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");
rdcproCommented:
Just to be clear, I gave you three examples of:

myProc.output = foo

You choose one of the three, as appropriate, and use it--not all three at the same time.  If you were doing this server-side, you'd probably write to the response stream directly, and call myProc.transform() at the point in the page where you want  the results to stream to the client.  

I guess I should proof read *before* I click submit!

Regards,
Mike Sharp
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

halfondjAuthor Commented:
My application is standalone and will consist of a server side.  I'm then assuming that I should just use:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.load("myXMLData.xml");

var xslDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xslDoc.async = false;
xslDoc.load("myStylesheet.xslt");

Also, I notice in all the examples that no one codes error handling.  Isn't it good to check if the the XML and XSL loaded?

Thanks.



 
rdcproCommented:
No, I'm saying that even in a standalone application, without server side processing, you still use the XSLTemplate approach.  At least I do.

Oh, and certainly you should code error handling.  I often leave it out of my examples to avoid confusion.  Download my transformation tool for some examples of that, including code that use the template processor approach.  Visit:

http://rdcpro.com/Members/rdcpro/tools

and download transformTool.zip.

In general, for error handling at a minimum I would check the parseError object after loading, and in general I put things in try-catch blocks to catch errors.  

As for the code I posted above, it is client side, though it is bare-bones.  You can't set parameters with the code you just posted.  I mentioned the server-side applications because it's essentially the same code--the only difference is where you send the output, and of course on the server you have to map the server path correctly.  

I'll point out one other detail, if you're doing client side transformations; it's not really necessary to use an output XML object:

var xslTemp = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
xslTemp.async = false;
xslTemp.validateOnParse = false;
xslTemp.load(xslDoc)
xslTemplate.stylesheet = xslTemp;
xslProc = xslTemplate.createProcessor();
xslProc.input = xmlDoc;
xslProc.transform;
targetXML1.innerHTML = xslProc.output;

Regards,
Mike Sharp
halfondjAuthor Commented:
Thanks again for the excellent description.  You certainly desearve all the points you get :).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.