Code examples

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:

Simple transformation

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

Using StyleSheet parameters

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

Combining two XML documents before transforming

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

History

(v2.8.5)
Separate caching of XML or XSL using bitflags. Bugfix when loading via HTTP and using caching. New optional argument for .appendToRoot(). New property .useXPath.
(v2.8.4)
Added support for ServerXMLHTTP when loading XML from URLs.
(v2.8.3)
Added a new constant: XMLOBJ_DEFAULT_VERSION and a new method: .appendToNode().
(v2.8.2)
Bugfix: Using .setVersion() worked, but would throw an error anyway. Additionally, version 4 PROGIDs were supported, but the "4.0" registered as "Not supported"...
(v2.8.1)
Bugfix: Using .replaceNode() in conjunction with the caching mechanisms threw an error about mixed threading models.
(v2.8)
Fixed some bugs with Version-Dependent ProgIDs, and loading via URLs. Added support for the MSXML4 Parser. Added the constants to the Reference section.
(v2.7)
New function: quickTransformXML, and a new constant: XMLOBJ_NO_ERRORS was added to the interface.
(v2.6)
Implemented .getNodeText().
(v2.6)
Fixed a bug with parameters that wasn't specified in the QueryString being transferred to the stylesheet as "undefined". Now only specified parameters are transferred, which allows for default values in the stylesheet.
(v2.5)
Implemented .removeNode() method.
(v2.5)
Uploaded the website.
(v2.5)
Implemented .saveXML(), .replaceNode() and .appendToRoot() methods. Removed interface description from xmlobject.asp.
(v2.2)
Implemented property .validateOnParse
(v2.1)
Refined handling of cached documents - if another page uses the same file, and sets the XMLOBJ_USE_XSLTEMPLATE flag, the XMLObject will look for a cached version before trying to load a fresh document.
(v2.0)
Reworked logic for the 3rd parameter of constructor to use bitflags. Enables use of XSLTemplate together with XMLOBJ_NO_TRANSFORM.
(v2.0)
Implemented .validate() method.
(v2.0)
Implemented access to .addParameter() and .addObject() in the XSLProcessor.
(v2.0)
Succeeded in implementing the XSLTemplate object INCLUDING parameters from QueryString being transferred to the XSLProcessor!!!
(v1.1)
Pieced the documentation together, which contains XML and XSLT - and they are using this object for showing...
(v1.0)
Moved interface functions inside constructor for better encapsulation.
(v1.0)
Fixed the seemingly incredibly buggy .xml + .xsl properties, as they hadn't really been implemented -:)
(v0.1)
Implemented the loading methods .loadXMLdoc() + .loadXSLdoc()
(v0.1)
First successful tests. Lots of minor bugtracking & -fixing.
(v0.1)
Project started. Interface layout. Coding.