Browsing XML/XSLT with HTA/Scripting Runtime

Introduction

The initial idea of this article was to bring you an simplistic example of using a Scripting Runtime Library, “click here and here, blah-blah-blah, thank you.” I began to write because of the need to make my colleagues’ and my scripts file-system-aware. This ability proved to be very useful for software prototyping purposes or for building some small utilities; of course, it shouldn’t be used on the Web (due to security/privacy reasons I’ll discuss later).

Back to the tool. Prior to writing it, I was deep in XML for several weeks, so what you see here is an XML/XSLT viewer/browser, enveloped in the form of an HTML Application. It helped me a lot when I was learning XML/XSL, now it aids <some other people> in rapid checking and bug tracking of large numbers of XSL templates; I hope it helps you too.

Of course, this little browser (I’ll call it “Xbrowser” further on) is in no way a replacement for any enterprise-grade development tool. It is just:

  • an interactive learning tool that illustrates the basics of XML handling with JScript/MSXML—for beginners in XML development; and, maybe
  • an example of using Microsoft Scripting Runtime Object Library—for developers of office tools and solutions; and, of course
  • a simple utility for validating XML documents and viewing XSLT output

A last-minute addition to this article was a JScript batch transformation tool that uses (most of) the techniques described here.

Before using either of these utilities, please read the disclaimers within the script files; by using these utilities, you agree with them.

Requirements

For this piece of code to work properly, you’ll need the following code packs:

  • The Common Dialog ActiveX Control provides a standard set of dialog boxes for operations such as opening and saving files, setting print options, and selecting colors and fonts. It is shipped with MS Visual Basic and MS Office 2000/XP products, or can be downloaded from the Microsoft Web site.
  • The Scripting Runtime Object Library is a Microsoft file system management solution, designed for use with scripting languages; it is an integral part of Microsoft Office 2000/XP. The library is also available for download at the Microsoft Web site.
  • Microsoft XML Core Services and/or SDK (versions 3.0 or 4.0). This can be downloaded from the Microsoft Web site.
  • To install some of the previously mentioned packages, you’ll probably need a CAB Extraction utility. You can download it from a Microsoft site.

How Everything Works

If you look inside the attached archive, you’ll see that “Xbrowser” is no more than an HTML form. You can see how to use it, and how the code works behind the scenes, step by step.

Folder browsing

Figure 1: Choose a folder where your XML files are located.

This part uses Shell object, specifically it’s BrowseForFolder method.

function BrowseFolder()
{
   // Accessing the Shell:
   var objShell = new ActiveXObject("Shell.Application");

   // Calling the browser dialog:
   var objFolder = objShell.BrowseForFolder(0, "Select a folder:",
                                            0);

   if (objFolder == null) return "";

   // Accessing the folder through the FolderItem object:
   var objFolderItem = objFolder.Items().Item();
   var objPath = objFolderItem.Path;

   var foldername = objPath;
   if (foldername.substr(foldername.length-1, 1) != "\\")
      foldername = foldername + "\\";

   // foldername is the actual folder name.

   ...
}

File browsing and enumeration

Figure 2: Choose a file.

There are two interesting things here:

    • Scripting.FileSystemObject is the main point of access to the file system. In short:
      FileSystemObject contains Drives collection
      Folders collection
      Files collection

      More by Author

      Get the Free Newsletter!

      Subscribe to Developer Insider for top news, trends & analysis

      Must Read