There isn't that much to learn, except from learning how to create the object and use its features, but before we do that, you'll need to know that XMLObject() uses the MSXML Parser and some of its extensions to the W3C's XML Specification, though anyone who's been using other XML Parsers should have no trouble tweaking the code to work with them.
There are several ways to utilize the XMLObject from within an ASP page, but the basic principles are to
1) Include the xmlobject.asp
file (this is assumed in all examples); 2) Create an instance of XMLObject(), using one of the constructors,
and 3) Get the transformed result, or perform some manipulations.
The code examples are all using JScript ASP. Here we go:
This example simply outputs the result of transforming links.xml
with the stylesheet
transform.xsl
. If errors occur (or the xml documents aren't well-formed), an error description is output instead.
<% var oXML = new XMLObject("links.xml", "transform.xsl"); Response.Write((oXML.error ? oXML.errorDesc : oXML.output)); %>
You can make it even simpler by using the quickTransformXML function that became available in version 2.7:
<%= quickTransformXML("links.xml", "transform.xsl") %>
— or if you'd like to avoid any errors that might occur:
<%= quickTransformXML("links.xml", "transform.xsl", XMLOBJ_NO_ERRORS) %>
In this example, a QueryString parameter is passed to the XSLT StyleSheet before transforming, which is a very useful technique which — quite frankly — is a major pain to perform "manually", because it involves creating an XSLTemplate Object from which you get an XSLProcessor Object — a compiled stylesheet — that can receive parameters to transfer to the stylesheet.
In the example, the QueryString has a variable named "sel", whose value gets transferred to the stylesheet variable "selectedLink". (In the XSLT Stylesheet you would refer to this with "$selectedLink").
This one uses the XSLTemplate object and caches the XML documents in Application variables which means that when you change these documents you must call the .asp page using them with the following QueryString: reloadprocessors=yes, atleast once after you've uploaded the changes.
<% var oXML = new XMLObject("links.xml", "transform.xsl", "sel->selectedLink"); Response.Write((oXML.error ? oXML.errorDesc : oXML.output)); %>
The manipulator methods of XMLObject allows for some basic editing functionality, using some shortcuts to the XMLDOM Interface methods. The implemented manipulators are .appendToRoot(), .removeNode() and .replaceNode(). We'll use replaceNode here, to demonstrate the process.
The first document contains an XML element named <Include-File-Here/>, which acts as a placeholder for the included second
document. When we load the first document along with the stylesheet, we use the XMLOBJ_NO_TRANSFORM constant to tell the XMLObject not to
perform the transform. Afterwards, we specify that our placeholder element is to be replaced with the contents of the document seconddoc.xml
.
Lastly, we call .transform(), and print the output to the client...
<% var oXML = new XMLObject("firstdoc.xml", "stylesheet.xsl", XMLOBJ_NO_TRANSFORM); oXML.replaceNode("seconddoc.xml", "//Include-File-Here"); oXML.transform(); Response.Write((oXML.error ? oXML.errorDesc : oXML.output)); %>
(More examples to come...)